Another static question

C

cppsks

The following seems to work for g++. Is it legal though? I was thinking that
ALL non-const statics have to be initialized in the source files somewhere.

#include <iostream>

namespace nvps
{
static bool hi = false;

class Whatever
{
public:
static void printme();
};
}

void
nvps::Whatever::printme()
{
cout << hi << endl;
hi = true;
cout << hi << endl;
}



int main()
{
nvps::Whatever::printme();
}

~/personal/C++/>a.out
0
1
 
V

Victor Bazarov

cppsks said:
The following seems to work for g++. Is it legal though?

What makes you think it can be illegal?
I was thinking that
ALL non-const statics have to be initialized in the source files somewhere.

I am not sure what uninitialised statics you're alluding to.
#include <iostream>

namespace nvps
{
static bool hi = false;

class Whatever
{
public:
static void printme();
};
}

void
nvps::Whatever::printme()
{
cout << hi << endl;
hi = true;
cout << hi << endl;
}



int main()
{
nvps::Whatever::printme();
}

~/personal/C++/>a.out
0
1

V
 
C

cppsks

Victor Bazarov said:
What makes you think it can be illegal?
somewhere.

I am not sure what uninitialised statics you're alluding to.

I thought that the statics have to be initialized in the source (.cc) file.
I guess that it may only pertain to class-level statics and not for the them
that are available/defined at the namespace level.
 
V

Victor Bazarov

cppsks said:
I thought that the statics have to be initialized in the source (.cc) file.

OK. I tried to make sense of that statement and it seems ambiguous to me.

Let me explain. "Statics have to be initialised in the source file". In
the context, when you say that you "thought that ...", you imply that the
statement is wrong. Or maybe you imply that whatever I wrote contradicts
this statement. Or perhaps that you now have information that makes you
change your views. So, which is it? If it's the former, then no, statics
don't have to be initialised [explicitly]. All statics are, in fact,
initialised upon program loading (or as part of program loading). So,
there is no need to initialise them. If the meaning of your "I thought
that..." is in fact that I said something to contradict this, what is it
you're referring to?
I guess that it may only pertain to class-level statics and not for the them
that are available/defined at the namespace level.

"Class-level statics" have to be initialised at the namespace level _iff_
they aren't initialised in the class definition. Also, consider that the
statement that puts a static class member in play is usually simply
a *declaration*, and as with any object, a *definition* is required if
the object is used elsewhere.

The last but not least note. In the source code you posted along with
your original question there are *no* uninitialised objects. Look at
it once again. That's partially why I said that I wasn't sure what
*uninitialised* statics you were alluding to.


V
 
C

cppsks

Victor Bazarov said:
OK. I tried to make sense of that statement and it seems ambiguous to me.

Let me explain. "Statics have to be initialised in the source file". In
the context, when you say that you "thought that ...", you imply that the
statement is wrong. Or maybe you imply that whatever I wrote contradicts
this statement. Or perhaps that you now have information that makes you
change your views. So, which is it? If it's the former, then no, statics
don't have to be initialised [explicitly]. All statics are, in fact,
initialised upon program loading (or as part of program loading). So,
there is no need to initialise them. If the meaning of your "I thought
that..." is in fact that I said something to contradict this, what is it
you're referring to?
I guess that it may only pertain to class-level statics and not for the them
that are available/defined at the namespace level.

"Class-level statics" have to be initialised at the namespace level _iff_
they aren't initialised in the class definition. Also, consider that the
statement that puts a static class member in play is usually simply
a *declaration*, and as with any object, a *definition* is required if
the object is used elsewhere.

The last but not least note. In the source code you posted along with
your original question there are *no* uninitialised objects. Look at
it once again. That's partially why I said that I wasn't sure what
*uninitialised* statics you were alluding to.


V

Victor,

Thanks for your response. Let me try to show another example:

#include <iostream>
namespace hi
{
static int namespaceStatic = 1; // allowed to be initialized here

class Hello
{
public:
static int classStatic = 10; // initialization is not allowed here
(see the compilation error below)
};
}
int main()
{
cout << hi::namespaceStatic << endl;
cout << hi::Hello::classStatic << endl;
}

/> g++ test.cc
test.cc:9: ANSI C++ forbids in-class initialization of non-const static
member `classStatic'


From the above example, it seems to me that you can initialize namespace
statics in the header while class-level statics MUST be initialized/defined
in the source file. Is that a fair statement to make?

Thanks,
Satish
 
V

Victor Bazarov

cppsks said:
[...] Let me try to show another example:

#include <iostream>
namespace hi
{
static int namespaceStatic = 1; // allowed to be initialized here

class Hello
{
public:
static int classStatic = 10; // initialization is not allowed here
(see the compilation error below)
};
}
int main()
{
cout << hi::namespaceStatic << endl;
cout << hi::Hello::classStatic << endl;
}

/> g++ test.cc
test.cc:9: ANSI C++ forbids in-class initialization of non-const static
member `classStatic'


From the above example, it seems to me that you can initialize namespace
statics in the header

Why is the word "header" here? In your example there is no header. And
in general, _headers_ are inconsequential. They are included into
translation units and become part of the translation units. There is no
sense to talk headers here.
while class-level statics MUST be initialized/defined
in the source file. Is that a fair statement to make?

Of course not. "Class-level statics" cannot be initialised in the class
definition if they are non-const. If a static data member is _const_ and
is of an integral type, it _may_ be initialised in the class definition.
It still has to be defined at the namespace level if used anywhere outside
the class itself. Any non-const static data member or any static data
member of a non-integral type has to be defined outside (if used in the
program).

The main distinction here is not "header" vs "source file". It is "the
class definition" vs "namespace". Where you put your code is not at all
important, as long as you satisfy the One Definition Rule.

V
 

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
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top