Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize static members in the header

Given is a class with a static member.

class BaseClass { public:     static std::string bstring; }; 

String has obviously to be default-initialized outside of the class.

std::string BaseClass::bstring {"."}; 

If I include the above line in the header along with the class, I get a symbol multiply defined error. It has to be defined in a separate cpp file, even with include guards or pragma once.

Isn't there a way to define it in the header?

like image 276
Appleshell Avatar asked Sep 17 '13 22:09

Appleshell


People also ask

Can we initialize static variable in header file?

No, it can't be done in a header - at least not if the header is included more than once in your source-files, which appears to be the case, or you wouldn't get an error like that.

How do you initialize a static member?

The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile . You cannot declare a static data member as mutable . You can only have one definition of a static member in a program.

What happens if u define static member variable in header file?

It will be considered as a global static variable. It will limit its scope within that file in which that header file has been included. Generally, we should not use it because whoever will include that header file in any number of source code, it will create separate variable copy for each source file.

How do you initialize a static object?

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 (::).


1 Answers

You can't define a static member variable more than once. If you put variable definitions into a header, it is going to be defined in each translation unit where the header is included. Since the include guards are only affecting the compilation of one translation unit, they won't help, either.

However, you can define static member functions! Now, at first sight that may not look as if it could help except, of course, that function can have local static variable and returning a reference to one of these behaves nearly like a static member variable:

static std::string& bstring() { static std::string rc{"."}; return rc; } 

The local static variable will be initialized the first time this function is called. That is, the construction is delayed until the function is accessed the first time. Of course, if you use this function to initialize other global objects it may also make sure that the object is constructed in time. If you use multiple threads this may look like a potential data race but it isn't (unless you use C++03): the initialization of the function local static variable is thread-safe.

like image 111
Dietmar Kühl Avatar answered Sep 23 '22 15:09

Dietmar Kühl