the essense of the static data member of a class

M

mangalalei

A static data member can be of the same class type as that of which it
is a member. A nonstatic data member is restricted to being declared as
a pointer or a reference to an object of its class.

And I haved used the sizeof operator to test a class which has a static
data member, the class size is all the nonstatic data member except the
static data member.

In compiler's view, is the static data member constructed after the
whole class constructed??And is the static data member just a "global"
variable in the class scope???
In compiler's view, the static data member actually is not the class
member, is that true???
 
B

benben

A static data member can be of the same class type as that of which it
is a member. A nonstatic data member is restricted to being declared as
a pointer or a reference to an object of its class.
Correct.


And I haved used the sizeof operator to test a class which has a static
data member, the class size is all the nonstatic data member except the
static data member.

It should be *at least* the sum of sizes of all non-static member
variables. Compiler can add more storage for things like padding and
type information, etc.
In compiler's view, is the static data member constructed after the
whole class constructed??And is the static data member just a "global"
variable in the class scope???

What do you mean by "construct" here?

If you mean instantiating, then it is done at runtime. And classes
themselves can't be constructed.

If you mean by compiling, then you might want to read some
compiler-related literature. Usually a compilation consists of several
passes. So by the time the compiler needs to emit code for the static
member variable it has all it needs to know about the class.

And yes, static member variables are much like namespace-scope variables
(including global variables.) Compiler needs only to enforce a set of
access rules for the use of it elsewhere in the code.
In compiler's view, the static data member actually is not the class
member, is that true???

What do you mean? And why do you care?

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top