static data member lost?

J

JustSomeGuy

I have a class that has a public data member.
If however I declare that data member as being static, then the linker
fails and says the symbol is undefined.
Must static data members be private?
 
V

Victor Bazarov

JustSomeGuy said:
I have a class that has a public data member.
If however I declare that data member as being static, then the linker
fails and says the symbol is undefined.
Must static data members be private?

No. But static data members have to be _defined_ at the namespace level.

V
 
J

JustSomeGuy

Victor said:
No. But static data members have to be _defined_ at the namespace level.

V

Ok I think I see...

so if I have a class


class myclass
{
public:
static int x;
};

main()
{
myclass c;
cout << std::c.x;
}


Is that correct?
 
V

Victor Bazarov

JustSomeGuy said:
Victor Bazarov wrote:




Ok I think I see...

so if I have a class


class myclass
{
public:
static int x;
};

main()
{
myclass c;
cout << std::c.x;
}


Is that correct?

No. First of all, myclass::x is still not defined anywhere. Second,
what's "std::c.x"? Why is 'c' prefixed with 'std::'? Your object is
not in the 'std' namespace, is it? Third, 'cout' is undefined. Did
you forget to include <iostream>, maybe? Fourth, 'main' is a function,
every function needs a return value type: "int main()".

To define myclass::x you need to add this line:

int myclass::x;

somewhere in the same namespace where 'myclass' is defined.

V
 
M

Mike Wahler

JustSomeGuy said:
Ok I think I see...

No, I don't think you do. See below.
so if I have a class


class myclass
{
public:
static int x;
};

main()
{
myclass c;
cout << std::c.x;
}


Is that correct?

No. Did you even try to compile that? It contains several errors.


#include <iostream>

class myclass
{
public:
static int x;
};

int myclass::x;

int main()
{
std::cout << myclass::x << '\n'; /* prints 0 */
return 0;
}

-Mike
 
K

Kante Mamadou Moustapha

Mike Wahler a écrit :
No, I don't think you do. See below.




No. Did you even try to compile that? It contains several errors.


#include <iostream>

class myclass
{
public:
static int x;
};

int myclass::x;

int main()
{
std::cout << myclass::x << '\n'; /* prints 0 */
return 0;
}

-Mike
and he should initialize it before using, yes you can say compiler do
it, but not sure, normalization say nothing about that, it depends on
the compiler.
 
R

Richard Herring

Kante Mamadou Moustapha said:
Mike Wahler a écrit :
and he should initialize it before using,

No need. Since it's static, it gets initialised.
yes you can say compiler do it, but not sure,

You may not be sure, but the standard is: "The storage for objects with
static storage duration (3.7.1) shall be zero-initialized (8.5) before
any other initialization takes place." (3.6.2/1)
normalization say nothing about that, it depends on the compiler.

???
 
M

Mike Wahler

Kante Mamadou Moustapha said:
Mike Wahler a écrit :
and he should initialize it before using,

Only if an initial value of zero is not acceptable.
yes you can say compiler do
it,

Yes, the language standard requires that it be initialized to
zero.
but not sure, normalization say nothing about that,

The ISO standard does, in no uncertain terms.
it depends on
the compiler.

Yes, it depends upon whether the compiler's behavior
conforms to the standard. If it does, the object will
be zero-initialized. Always.

-Mike
 

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

Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top