473,372 Members | 859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

initialization of static variable inside a structure

hi
i have a class in which i have a structure.. how to initialize a static variable "var"

Class A
{

struct start
{
static int var;
};

};

actually when i am doing like
A::start::var =1;
its giving undefined reference error
Mar 20 '08 #1
1 7396
weaknessforcats
9,208 Expert Mod 8TB
Static member variables are not part of any one struct variable. All the struct variables will use the same static variable. Therefore, the static variable has to be created outside the struct:
Expand|Select|Wrap|Line Numbers
  1. Class A
  2. {
  3.  
  4. struct start
  5. {
  6. static int var;
  7. };
  8.  
  9. };
  10.  
  11. //in a .cpp ffile
  12.  
  13. int A::start::var;
  14.  
Please note that in C++ class and struct are interchangeable. That is, if your members are declared as public/private/protected, there is no difference between a class and a struct. The single difference is when you do not specify public/private/protected, the default access for a class is private whereas the default access for a struct is public. Underneath it all, classes are implemented as structs.
Mar 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
6
by: Roman | last post by:
Hi, Does anyone know why in C++, it is ok to initialize a const static int inside a class, but you can't initialize a float? From what I understand, initialization of const static float can...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
14
by: seema | last post by:
Hi all, I am new to C++ programming. Can some body explain how to intialize the structure values, consider this program, #include <iostream.h> struct mystruct { int a,b,c,d,e,f; };
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
5
by: Triple-DES | last post by:
I've seen this question raised more than once, but I have not yet seen a definite, conclusive answer. Consider the following code: struct C { C() {} // might throw }; int main() { try {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.