473,473 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

static variable:declare and define

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?

Apr 6 '06 #1
1 1679
li*****@hotmail.com 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
Apr 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
2
by: Michelle | last post by:
Hi all, I have a query that scans huge table consists of 8 or more millions records. The funny thing is that if I use the query with local variable, the query takes more than 1 minutes,...
6
by: Dipak Patel | last post by:
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000 I wish to store the numeric result of a query into a variable, as described below. This does not work, it fails on the last line with...
8
by: nilavya | last post by:
Hi Gurus, Since I learned C++, way back in 2002, I always had a doubt about the different types of storage in C++. We have "const", "static" , "#define" for one or other purpose. Now I have a...
2
by: Ishaan | last post by:
Hi, Is it possible to declare a variable dynamically? Anwar -- Software engineer Petroleum Development Oman
8
by: pozz | last post by:
In my software, there are some variables that represents some settings. They usually are numerical variables: unsigned char (0..255), signed char (-127..128), unsigned int (0..65535), signed int...
1
by: kiluyar | last post by:
I have such a function: class T { public: T(){...//some operations} }; T& GetInst()
14
by: cs1975 | last post by:
Hi Everyone, I wanted to make a global variable double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; to static global static double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; I am updating this global...
1
by: little83 | last post by:
Dear all I got these error when I declare a variable inside one of the methods Error 47 Use of unassigned local variable 'm' However i don't get these error when I declare it at the top of the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.