468,512 Members | 1,508 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 5278
weaknessforcats
9,207 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

Post your reply

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

Similar topics

6 posts views Thread by Neil Zanella | last post: by
6 posts views Thread by Roman | last post: by
30 posts views Thread by questions? | last post: by
14 posts views Thread by seema | last post: by
7 posts views Thread by Spoon | last post: by
16 posts views Thread by RB | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.