Click here to Skip to main content
13,510,493 members
Rate this:
Please Sign up or sign in to vote.
See more:
#include <iostream>
using namespace std;
class A{
    static int x;
    static A y;
A::A() {
A A::y=*new A;
int A::x=5;
void main(){
    A a;
    A b;

hello guys
i camt understand this code .... what does it mean to do this (
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 19-Jan-13 6:22am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:


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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^]

As Philippe points out below, the initialisation of static objects takes place before any other code is executed.
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...
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
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" :).
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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180417.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100