By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

Some questions about static member

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tran Tuan Anh wrote:
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.
Jul 23 '05 #2

P: n/a
Tran Tuan Anh schrieb:
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
Jul 23 '05 #3

P: n/a
Thanks all for your prompt replies. I kind of understand now :)

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.