C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static data member lost?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
JustSomeGuy
Guest





PostPosted: Thu Oct 28, 2004 7:48 pm    Post subject: static data member lost? Reply with quote



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?



Back to top
Victor Bazarov
Guest





PostPosted: Thu Oct 28, 2004 7:53 pm    Post subject: Re: static data member lost? Reply with quote



JustSomeGuy wrote:
Quote:
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

Back to top
JustSomeGuy
Guest





PostPosted: Thu Oct 28, 2004 8:17 pm    Post subject: Re: static data member lost? Reply with quote



Victor Bazarov wrote:

Quote:
JustSomeGuy wrote:
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

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?




Back to top
Victor Bazarov
Guest





PostPosted: Thu Oct 28, 2004 8:42 pm    Post subject: Re: static data member lost? Reply with quote

JustSomeGuy wrote:
Quote:
Victor Bazarov wrote:


JustSomeGuy wrote:

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


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:Mad 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 every function needs a return value type: "int main()".

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

int myclass:Mad;

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

V

Back to top
Mike Wahler
Guest





PostPosted: Fri Oct 29, 2004 1:30 am    Post subject: Re: static data member lost? Reply with quote


"JustSomeGuy" <NoOne (AT) ucalgary (DOT) ca> wrote

Quote:
Victor Bazarov wrote:

JustSomeGuy wrote:
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

Ok I think I see...

No, I don't think you do. See below.

Quote:

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
class myclass
{
public:
static int x;
};

int myclass:Mad;

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

-Mike

Quote:






Back to top
Kante Mamadou Moustapha
Guest





PostPosted: Fri Oct 29, 2004 6:20 am    Post subject: Re: static data member lost? Reply with quote

Mike Wahler a écrit :
Quote:
"JustSomeGuy" <NoOne (AT) ucalgary (DOT) ca> wrote in message
news:418153E3.C8B51043 (AT) ucalgary (DOT) ca...

Victor Bazarov wrote:


JustSomeGuy wrote:

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

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
class myclass
{
public:
static int x;
};

int myclass:Mad;

int main()
{
std::cout << myclass:Mad << '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.

Back to top
Richard Herring
Guest





PostPosted: Fri Oct 29, 2004 9:35 am    Post subject: Re: static data member lost? Reply with quote

In message <clsqk9$3ne$2 (AT) news (DOT) u-bordeaux.fr>, Kante Mamadou Moustapha
<mkante (AT) emi (DOT) u-bordeaux.fr> writes
Quote:
Mike Wahler a écrit :
"JustSomeGuy" <NoOne (AT) ucalgary (DOT) ca> wrote in message
news:418153E3.C8B51043 (AT) ucalgary (DOT) ca...

Victor Bazarov wrote:


JustSomeGuy wrote:

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

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 class myclass
{
public:
static int x;
};
int myclass:Mad;
int main()
{
std::cout << myclass:Mad << 'n'; /* prints 0 */
return 0;
}
-Mike




and he should initialize it before using,

No need. Since it's static, it gets initialised.

Quote:
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)

Quote:
normalization say nothing about that, it depends on the compiler.

???

--
Richard Herring

Back to top
Mike Wahler
Guest





PostPosted: Fri Oct 29, 2004 4:38 pm    Post subject: Re: static data member lost? Reply with quote


"Kante Mamadou Moustapha" <mkante (AT) emi (DOT) u-bordeaux.fr> wrote

Quote:
Mike Wahler a écrit :
"JustSomeGuy" <NoOne (AT) ucalgary (DOT) ca> wrote in message
news:418153E3.C8B51043 (AT) ucalgary (DOT) ca...

Victor Bazarov wrote:


JustSomeGuy wrote:

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

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
class myclass
{
public:
static int x;
};

int myclass:Mad;

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

-Mike






and he should initialize it before using,

Only if an initial value of zero is not acceptable.

Quote:
yes you can say compiler do
it,

Yes, the language standard requires that it be initialized to
zero.

Quote:
but not sure, normalization say nothing about that,

The ISO standard does, in no uncertain terms.

Quote:
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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.