Connecting Tech Pros Worldwide Forums | Help | Site Map

Some questions about static member

Tran Tuan Anh
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Some questions about static member


Tran Tuan Anh wrote:
[color=blue]
> 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?[/color]

Because that's how C++ is defined.
[color=blue]
> then I move the initialization outside to A.cpp:
>
> A::instance = 0;[/color]

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.
[color=blue]
> then compiler complains and I have to do the following:
>
> A* A::instance = 0;
>
> Here I also don't see why.[/color]

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.
[color=blue]
> For example, if I have an interger:
>
> int i;
>
> then I initialize it by:
>
> i = 0;[/color]

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
[color=blue]
> that is it. Why the instance var above need the A* type declearation.[/color]

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

I hope I could help a bit.
Malte Starostik
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Some questions about static member


Tran Tuan Anh schrieb:[color=blue]
> 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?[/color]

That's only allowed for const integral or enumeration types, instance
above is neither const nor an integral type.
[color=blue]
> 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;[/color]

Compare the two again:
type name
int i;
A* A::instance;
[color=blue]
>
> then I initialize it by:
>
> i = 0;[/color]

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

HTH,
Malte
anhtt@hotmail.com
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Some questions about static member


Thanks all for your prompt replies. I kind of understand now :)

Closed Thread