Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize private static members in C++?

What is the best way to initialize a private, static data member in C++? I tried this in my header file, but it gives me weird linker errors:

class foo {     private:         static int i; };  int foo::i = 0; 

I'm guessing this is because I can't initialize a private member from outside the class. So what's the best way to do this?

like image 403
Jason Baker Avatar asked Oct 09 '08 03:10

Jason Baker


People also ask

How do I initialize private static?

For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value. The following code will illustrate the of static member initializing technique.

Can we create private static characteristics of a class?

It is possible to declare a data member of a class as static irrespective of it being a public or a private type in class definition. If a data is declared as static, then the static data is created and initialized only once.

How do you declare and initialize static data members in a class?

When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

Where do you initialize static variables?

As static variables are initialized only once and are shared by all objects of a class, the static variables are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).


2 Answers

The class declaration should be in the header file (Or in the source file if not shared).
File: foo.h

class foo {     private:         static int i; }; 

But the initialization should be in source file.
File: foo.cpp

int foo::i = 0; 

If the initialization is in the header file then each file that includes the header file will have a definition of the static member. Thus during the link phase you will get linker errors as the code to initialize the variable will be defined in multiple source files. The initialisation of the static int i must be done outside of any function.

Note: Matt Curtis: points out that C++ allows the simplification of the above if the static member variable is of const int type (e.g. int, bool, char). You can then declare and initialize the member variable directly inside the class declaration in the header file:

class foo {     private:         static int const i = 42; }; 
like image 168
Martin York Avatar answered Oct 22 '22 10:10

Martin York


For a variable:

foo.h:

class foo { private:     static int i; }; 

foo.cpp:

int foo::i = 0; 

This is because there can only be one instance of foo::i in your program. It's sort of the equivalent of extern int i in a header file and int i in a source file.

For a constant you can put the value straight in the class declaration:

class foo { private:     static int i;     const static int a = 42; }; 
like image 33
Matt Curtis Avatar answered Oct 22 '22 10:10

Matt Curtis