Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
C#
#include <iostream>
using namespace std;
class A{
public:
    static int x;
    static A y;
    A();
    ~A();
};
A::A() {
    y.x++;
}
A::~A()
{
    y.x--;
}
A A::y=*new A;
int A::x=5;
void main(){
    A a;
    A b;
    cout<<a.x<<endl;
    cout<<b.x<<endl;
}

hello guys
i camt understand this code .... what does it mean to do this (
C#
A A::y=*new A;
int A::x=5;

) and what's the point of having a static object of the class in side it ....
even after watching the variables ... i couldnt understand this
any help please .. :'(
Posted

In addition to answer 1 and 2, it is important to notice that since A constructor increment x static member, it is important to be aware that constant expression are initialized before dynamic initialization.

Thus a is initialized to 5 first.
Then the initialization of y will cause x to increment to 6.

Then in main, objects a and b will cause x to be incremented twice. Thus the value would be 8 after both constructors are called.

Since x is static both cout statements will output the same variable which will hold 8 at that point. Thus the output will be:

8
8


Finally it is interesting to note that since the copy constructor is not defined, counting would not balance if the copy constructor would have been used.

Also since there is a new used to initialize a static variable there will be no corresponding destructor called at the end of the programm.

When main is exited, destructors for b and a are called thus x will have a value of 6 when the program is about to exit.

In this case it does not matter much but if the counter would have been used for ressources that must be explicitly released (typically out-of-process ressources), then it would have been problematic.
 
Share this answer
 
C++
A A::y=*new A;
int A::x=5;

These statements initialise the static variables: x is intialised to 5, and y is initialised to a new instance of an A object. You should be able to see how the other variables are initialised and changed by stepping through the code.

You would also be well advised to get hold of a good book or tutorial on C++. See also Bjarne Stroustrup's homepage[^]

[edit]
As Philippe points out below, the initialisation of static objects takes place before any other code is executed.
[/edit]
 
Share this answer
 
v2
Comments
H.Brydon 19-Jan-13 13:12pm    
Correct and complete... Gave +5 to boost somebody else's +4...
Richard MacCutchan 19-Jan-13 13:30pm    
Thank you, but all I did was compile it and step through the program.
Sergey Alexandrovich Kryukov 19-Jan-13 19:24pm    
Agree, a 5. Good idea to reference the site...
—SA
Philippe Mori 19-Jan-13 22:15pm    
Although the answer is good, it might be somewhat clearer about the fact that constant expression initialization is done before dynamic initialization...
Richard MacCutchan 20-Jan-13 3:27am    
Thank you, a good point.
C++
A A::y=*new A;

The A class contain a static field of type A, with name y. This statement creates a new instance of A and assigns it to the static field of the class itself.
C++
int A::x=5;

x is also a static member of A, it is assigned the value 5. This is used as an instance counter. If you look at the code, you will see, that every time an instance is created, the counter is incremented, and decremented on destruction. The inner instance is only used to access the static counter, but this is useless in general.
As I see, this is a code to think about, with no actual use. So the answer to "why" is actually "why not" :).
 
Share this answer
 
Comments
SajeeshCheviry 19-Jan-13 13:49pm    
hm. good answer. my vote of 5
Zoltán Zörgő 19-Jan-13 15:43pm    
Thank you.
Sergey Alexandrovich Kryukov 19-Jan-13 19:23pm    
Sure, a 5.
—SA
Philippe Mori 19-Jan-13 22:17pm    
It might be important to mention that constant expression are initialized before dynamic initilization as in this case, it will affect the output as A constructor modify x.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900