Some questions about static member

T

Tran Tuan Anh

Dear all,

I am new with C++ and very confused with some features.
Really appreciate if you can explain to me some of stuffs below.

I define a class:

class A {
static A* instance = 0;
};

then I have error saying that I cannot initialize a static member
inside the class. Why? As a newbie to C++ I don't see why not?

then I move the initialization outside to A.cpp:

A::instance = 0;

then compiler complains and I have to do the following:

A* A::instance = 0;

Here I also don't see why. For example, if I have an interger:

int i;

then I initialize it by:

i = 0;

that is it. Why the instance var above need the A* type declearation.

Basically, as a newbie to C++ I am very confused with huge syntaxs and
semantics. Really want to learn more.

Many thanks in advance!

Tuan-Anh
 
R

Rolf Magnus

Tran said:
Dear all,

I am new with C++ and very confused with some features.
Really appreciate if you can explain to me some of stuffs below.

I define a class:

class A {
static A* instance = 0;
};

then I have error saying that I cannot initialize a static member
inside the class. Why? As a newbie to C++ I don't see why not?

Because that's how C++ is defined.
then I move the initialization outside to A.cpp:

A::instance = 0;

That wouldn't be an initialization. It would be an assignment, and
assignments are not allowed outside of a function.
The difference between initialization and assignment is that the former
creates a new object and gives it the specified value, whereas an
assignment just gives a new value to an already existing object.
then compiler complains and I have to do the following:

A* A::instance = 0;

Here I also don't see why.

Because in the class definition, you only declare 'instance', i.e. you say
that the class has a member with that name. But it isn't yet definied, i.e.
there is no storage for that object. Therefore, you have to add to one
translation unit a definition of that static member variable.
For example, if I have an interger:

int i;

then I initialize it by:

i = 0;

Again, that's not an initialization, but an assignment. You can tell the
difference by the following rule:

type name = value; <- Initialization
name = value; <- Assignment
that is it. Why the instance var above need the A* type declearation.

It's unclear to me what you mean.
Basically, as a newbie to C++ I am very confused with huge syntaxs and
semantics. Really want to learn more.

I hope I could help a bit.
 
M

Malte Starostik

Tran said:
Dear all,

I am new with C++ and very confused with some features.
Really appreciate if you can explain to me some of stuffs below.

I define a class:

class A {
static A* instance = 0;
};

then I have error saying that I cannot initialize a static member
inside the class. Why? As a newbie to C++ I don't see why not?

That's only allowed for const integral or enumeration types, instance
above is neither const nor an integral type.
then I move the initialization outside to A.cpp:

A::instance = 0;

then compiler complains and I have to do the following:

A* A::instance = 0;

Here I also don't see why. For example, if I have an interger:

int i;

Compare the two again:
type name
int i;
A* A::instance;
then I initialize it by:

i = 0;

type name initialiser
int i = 0;
A* A::instance = 0;
that is it. Why the instance var above need the A* type declearation.
Just like you need int i = 0; and not just i = 0; Don't let the ::
confuse you.

HTH,
Malte
 

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,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top