Re: non-const static member variale initialization

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks Bo Persson,

This is initializing the variable, not defining it. The definition is
on the previous line.

{
}
}


All together, this is a class definition, because it defines the
class.

Actually, I do not quite agree with you (correct me if I am wrong). I think
definition involves storage space allocation. When in a class we write int i
as a data member variable and make it public, actually the 4-byte storage is
not allcoated. The storage space is allocated in constuctor. So, my point is
I think the definition of member variable i is missing. :-)

Any comments?


regards,
George

"Bo Persson" wrote:

George wrote:
Thanks Bo Persson,


Your reply is clear. I am confused what code in class could be
viewed as definition of a member variable. For code like this,

class Foo {
public:
int i;
Foo (int input): i (input)

This is initializing the variable, not defining it. The definition is
on the previous line.

{
}
}


All together, this is a class definition, because it defines the
class.

(On the next line I would expect either the name of a class object or
a semicolon :-).

What code do you think is member variable i definition?

A variable definition looks like this

int i;

whether it is a member of a class or not. :-)


How about code like this?

class Foo {
public:
int i;
Foo (int input)
{
i = input;

This is assigning a value to an uninitialized member variable.

}
}



Bo Persson



.



Relevant Pages