Alex Rivera | Logout

static class member of class's own type

Asked 2012-03-28T06:42:52.110
9

Possible Duplicate:
Do static members of a class occupy memory if no object of that class is created?
Memory Allocation of Static Members in a Class

"A class is not considered defined untill its class body is complete, a class can not have data members of its own type. A class can have data members that are pointers/reference to its own type."

  • C++ Primer (Lippman Lajoie)

Makes sense.

But why is this allowed then ?

class justAClass
{
     public  : 
     justAClass();

     private :          
     static justAClass justAMember;
}

For pointers it is understandable. But how will this above thing work ? How will i ever decide the size for object of such a class ? Isnt it a recursive case (with no base condition) to have a member of its own type, even if it is static ?

Edit
Report

1 Answer

3

Formally, the distinction is that the declaration of a static member in a class is not a definition. You must provide a definition elsewhere (exactly once), and the compiler doesn't need to know the size until it encounters the definition. Static members do not impact on the size of the class itself. (In many ways, the static member declaration in the class is very much like an extern non-member declaration.)

answered 2012-03-28T07:37:39.010

Your Answer