static data members

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
following is mentioned in page 469 section '12.6.2 static Data
members':
Unlike ordinary data members, static members are not initialized
through class constructor(s) and instead should be initialized when
they are defined.

I do not understand this sentence. Kinldy explain what it means with
example.

Thanks
V.Subramanian
 
T

Tim Love

In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
following is mentioned in page 469 section '12.6.2 static Data
members':
Unlike ordinary data members, static members are not initialized
through class constructor(s) and instead should be initialized when
they are defined.
I do not understand this sentence. Kinldy explain what it means with
example.

I think it means that this works

class a {
public:
int i;

a(){ i=7;};

};

int main() {
a x;
}


but this doesn't

class a {
public:
static int i;

a(){ i=7;};

};

int main() {
a x;
}



However, this does


class a {
public:
static int i;
};

int a::i=7;

int main() {
a x;
}
 
G

Gert-Jan de Vos

In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
following is mentioned in page 469 section '12.6.2 static Data
members':
Unlike ordinary data members, static members are not initialized
through class constructor(s) and instead should be initialized when
they are defined.

I do not understand this sentence. Kindly explain what it means with
example.

Suppose we have this class definition:

class X
{
public:
X();

private:
int m_a;
static int m_b;
};

Now each new instance of type X, like:

X x;

Will be initialised by X's constructor like this:

X::X() : m_a(12)
{
}

So, each new instance of X has its own instance of its member
variables
that can be initialised when the object is first created by the class'
constructor.

m_b on the other hand is a static member variable. static members
belong
to the class rather than to its instances. There is always a single
instance of a static member, whether there are zero, one or many
instances
of the class. This means that a static member can only be initialised
once.
That is not in the per instance constructor, but like this:

int X::m_b = 100;

This initialisation is performed once before the entry of main().
 
S

SG

In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
following is mentioned in page 469 section '12.6.2 static Data
members':
Unlike ordinary data members, static members are not initialized
through class constructor(s) and instead should be initialized when
they are defined.

I do not understand this sentence. Kinldy explain what it means with
example.

"when they are defined" is not the same as "when they are declared".
Is it possible that you confuse these two?

class foo {
public:
static int j; // NOT a definition, just a declaration
};

...

int foo::j; // Definition (allocates memory in TU)

C++ allows you to use an initializer as well. You can replace the last
line with

int foo::j = 23;


Cheers,
SG
 
J

Jerry Coffin

In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
following is mentioned in page 469 section '12.6.2 static Data
members':
Unlike ordinary data members, static members are not initialized
through class constructor(s) and instead should be initialized when
they are defined.

I do not understand this sentence. Kinldy explain what it means with
example.

A normal data member is initialized by the ctor for the class. A
static data member is basically a global variable with a strange name
-- the name of the class, a double colon, then the name of the
variable itself. Despite the oddity of the name, it still has to be
defined at global scope just like any other global variable:

class XXX {
// this defines the data member xxx
int xxx;

// this only _declares_ the static member yyy
static int yyy;

public:
// Here we initialize xxx
XXX() : xxx(123) {}
};

// Here we define XXX::yyy, and initialize it with the value 456
int XXX::yyy = 456;
 

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

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top