Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static variable:declare and define

Reply
Thread Tools

static variable:declare and define

 
 
linq936@hotmail.com
Guest
Posts: n/a
 
      04-06-2006
Hi,
I am confused at static variable in declaring and defining.

I have the following code in header file,

class c0;

class c1 {
static std::vector<c0> cs;
};

And in C file, I operate on c1::cs in some function. But VC7 compiler
complains that c1::cs is not initialized.

I have to add the following line to the top of the C code,
std::vector<c0> c1::cs;
Then it works.

This makes half sense to me. Static variable must be defined in
addition to declaring. But how come the above newly statement does the
definition? Is that because std::vector does some dummy initialization?

Then I have another header file,
class MyString {...};

static MyString myStr; // This variable does not belong to any
class

Then in the C file when I have
myStr = "";
The compiler complains that I am re-defining myStr.

I wonder what is going on? Because myStr is a standalone variable, so
its definition is defferent from class member?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-06-2006
wrote:
> I am confused at static variable in declaring and defining.
>
> I have the following code in header file,
>
> class c0;
>
> class c1 {
> static std::vector<c0> cs;


That's the declaration.

> };
>
> And in C file, I operate on c1::cs in some function. But VC7 compiler
> complains that c1::cs is not initialized.
>
> I have to add the following line to the top of the C code,
> std::vector<c0> c1::cs;


That's the definition.

> Then it works.
>
> This makes half sense to me. Static variable must be defined in
> addition to declaring.


Static member variables must. And only if they are of non-integral type
and are used outside the class itself.

> But how come the above newly statement does the
> definition?


Because it's outside of the class, at the namespace level.

> Is that because std::vector does some dummy
> initialization?


Nope. It's because the rules require it.

> Then I have another header file,
> class MyString {...};
>
> static MyString myStr; // This variable does not belong to any
> class


Then it's not a _member_, is it?

> Then in the C file when I have
> myStr = "";
> The compiler complains that I am re-defining myStr.


Huh? You must be including the header in more than one translation unit
and that introduces multiple _definitions_ in your program.

> I wonder what is going on? Because myStr is a standalone variable, so
> its definition is defferent from class member?


Yep. You got it!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to define and initialize the protected static members ? Timothy Madden C++ 1 11-21-2008 11:40 PM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
#define and external static Fan Zhang C Programming 8 10-14-2004 11:50 AM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments