initialize static variable in cpp file
Hey guys, here is what I read in CProgramming.com:
You cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file instead.
Have two questions regarding to this:
1. Kind of curious why it cannot be in header file. As long as the initialization does not happen inside the class should be fine right?
2. Also, can initialization happen in a static member function of the class? If not why?
Thank you so much!
Re: initialize static variable in cpp file
If you initialise the static member in the header file then it would exist as a different copy in every translation unit that included the header file. That is why it must be in the .cpp file. The only exception to this is a static member of a template class, that gets defined in the header file and its up to the linker to sort it out. This has to happen as a template definition in a .cpp file would be inaccessible.
In answer to the second question the only statics that can be initialised within the class are static const primitives. For other class statics they would need be in the .cpp file. The constructors cant initialise statics as statics appear once per class and constructors initialise on a per object basis.
Re: initialize static variable in cpp file
I'm not sure the second question was actually answered. Static members can never be "initialized" inside of a static member function. You can change them (if they aren't const) inside of static member functions but they have to be initialized prior to any static member functions being called. It doesn't make logical sense to allow "initialization" inside of a static member function because there is no guarantee as to which static member function would be called first. Object instances are created via constructors. In that case the constructor runs before any non-static member function so only constructors can initialize non-static members. You see the point? static members are shared by all object instances so there is no function or constructor that could initialize a static member. Therefore it must be done some other way.
Re: initialize static variable in cpp file
Quote:
Originally Posted by
Russco
If you initialise the static member in the header file then it would exist as a different copy in every translation unit that included the header file. That is why it must be in the .cpp file. The only exception to this is a static member of a template class, that gets defined in the header file and its up to the linker to sort it out. This has to happen as a template definition in a .cpp file would be inaccessible.
In answer to the second question the only statics that can be initialised within the class are static const primitives. For other class statics they would need be in the .cpp file. The constructors cant initialise statics as statics appear once per class and constructors initialise on a per object basis.
I see:) Thank you very much!
Re: initialize static variable in cpp file
Quote:
Originally Posted by
kempofighter
I'm not sure the second question was actually answered. Static members can never be "initialized" inside of a static member function. You can change them (if they aren't const) inside of static member functions but they have to be initialized prior to any static member functions being called. It doesn't make logical sense to allow "initialization" inside of a static member function because there is no guarantee as to which static member function would be called first. Object instances are created via constructors. In that case the constructor runs before any non-static member function so only constructors can initialize non-static members. You see the point? static members are shared by all object instances so there is no function or constructor that could initialize a static member. Therefore it must be done some other way.
Thank you for the clear explanation. I learnt a lot:)
Re: initialize static variable in cpp file
Quote:
Originally Posted by
lqakane
You cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file instead.
This is not entirely accurate. If the static data member is of a
const integral or const enumeration type, its declaration in the class
definition can specify a constant initializer. However, you must
still DEFINE the variable.
Example:
Code:
// in header file
class MyClass
{
static const int Size = 10;
int arr[Size];
};
// in cpp file
const int MyClass::Size; // must still be defined
Re: initialize static variable in cpp file
Quote:
Originally Posted by
Philip Nicoletti
Code:
// in header file
class MyClass
{
static const int Size = 10;
int arr[Size];
};
// in cpp file
const int MyClass::Size; // must still be defined
Actually, this will produce an error:
error LNK2005: "public: static int const MyClass::Size" (?Size@MyClass@@2HB) already defined in MyClass.obj
Also, how did this thread shift to static const member declaration? The OP's question was about static member's INITIALIZATION!
Re: initialize static variable in cpp file
Quote:
Originally Posted by
VladimirF
Actually, this will produce an error:
error LNK2005: "public: static int const MyClass::Size" (?Size@MyClass@@2HB) already defined in MyClass.obj
Not if you ignore the part about putting
Code:
const int MyClass::Size; // must still be defined
inside the cpp file. :D
Re: initialize static variable in cpp file
Quote:
Originally Posted by
VladimirF
Actually, this will produce an error:
error LNK2005: "public: static int const MyClass::Size" (?Size@MyClass@@2HB) already defined in MyClass.obj
Also, how did this thread shift to static const member declaration? The OP's question was about static member's INITIALIZATION!
1) As mentioned in previous post, the definition goes in the CPP file
(as in my comment)
2) My post did give initialization of the static member. It showed
how it does not always have to be in the implementation file.
Re: initialize static variable in cpp file
Just remember:
There can be only one!
A static variable is created once and only once. Russco provided a good explanation of why this is the case.
Re: initialize static variable in cpp file
Quote:
Originally Posted by
VladimirF
Also, how did this thread shift to static const member declaration? The OP's question was about static member's INITIALIZATION!
The OP made an inaccurate statement that static member variables couldn't be initialized in the class declaration. Some of us were simply pointing out that the statement was incorrect and that they can be in limited cases. I think that all of the discussions here were relevant to the OP. You are correct about the linker error though. If the member is initialized in the header than you can't redefine it and reinitialize it in the .cpp as well.
Re: initialize static variable in cpp file
Quote:
Originally Posted by
lqakane
Hey guys, here is what I read in CProgramming.com:
Well what do you expect from CProgramming.com? I recommend these websites for learning a little c++.
http://www.cplusplus.com/
http://www.research.att.com/~bs/homepage.html
http://www.parashift.com/c++-faq-lite/index.html
Re: initialize static variable in cpp file
Quote:
Originally Posted by
PredicateNormative
Not if you ignore the part about putting
Code:
const int MyClass::Size; // must still be defined
inside the cpp file. :D
Actually, I did what you've suggested.
My mistake is - I am using MS extensions to C++ that makes out-of-class definition of static const members "optional".
When these extensions are enabled, you'd get the error I've mentioned.
Sorry for the confusion.
Re: initialize static variable in cpp file
Quote:
Originally Posted by VladimirF
Actually, this will produce an error:
error LNK2005: "public: static int const MyClass::Size" (?Size@MyClass@@2HB) already defined in MyClass.obj
I was not aware of that particular bug in the Visual C++ compiler.
Quote:
Originally Posted by
PredicateNormative
Not if you ignore the part about putting
Code:
const int MyClass::Size; // must still be defined
inside the cpp file. :D
Of course, you can not ignore that as it is required by the standard.
But it looks like that is the only choice for VC++ to get around the bug.
Re: initialize static variable in cpp file
Quote:
Originally Posted by
Philip Nicoletti
Of course, you can not ignore that as it is required by the standard.
:confused:
I'm not saying that you're wrong, perhaps you are right, but I can't find this in the standard, or at least not in the 2005 draft that I have.:ehh: