 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
JustSomeGuy Guest
|
Posted: Thu Oct 28, 2004 7:48 pm Post subject: static data member lost? |
|
|
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
|
Posted: Thu Oct 28, 2004 7:53 pm Post subject: Re: static data member lost? |
|
|
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
|
Posted: Thu Oct 28, 2004 8:17 pm Post subject: Re: static data member lost? |
|
|
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
|
Posted: Thu Oct 28, 2004 8:42 pm Post subject: Re: static data member lost? |
|
|
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: 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: you need to add this line:
int myclass: ;
somewhere in the same namespace where 'myclass' is defined.
V
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Fri Oct 29, 2004 1:30 am Post subject: Re: static data member lost? |
|
|
"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: ;
int main()
{
std::cout << myclass: << 'n'; /* prints 0 */
return 0;
}
-Mike
|
|
| Back to top |
|
 |
Kante Mamadou Moustapha Guest
|
Posted: Fri Oct 29, 2004 6:20 am Post subject: Re: static data member lost? |
|
|
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: ;
int main()
{
std::cout << myclass: << '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
|
Posted: Fri Oct 29, 2004 9:35 am Post subject: Re: static data member lost? |
|
|
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: ;
int main()
{
std::cout << myclass: << '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
|
Posted: Fri Oct 29, 2004 4:38 pm Post subject: Re: static data member lost? |
|
|
"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: ;
int main()
{
std::cout << myclass: << '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 |
|
 |
|
|
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
|
|