Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do non-const, non-int/enum static data members have to be initialized outside the definition?

Tags:

c++

static

header

I understand that only data members which are static, const and int/enum (pre c++11) can be initialized inside the class declaration. "All other static data members must be defined at global namespace scope (i.e. outside the body of the class definition) and can be only initialized in those definitions".

Why can't other static data members be initialized in the class definition? Was there a specific reason this was forbidden?

If the data members are specific to the class, why are they declared at the global namespace scope and not some scope relevant to their class?

like image 487
user997112 Avatar asked Mar 01 '13 21:03

user997112


People also ask

Why static member variable must be defined outside a class?

Because static member variables are not part of the individual class objects (they are treated similarly to global variables, and get initialized when the program starts), you must explicitly define the static member outside of the class, in the global scope.

Why definition of static data member is necessary?

The exception is that static data members must be defined in file scope regardless of their access restrictions. If the data member is to be explicitly initialized, an initializer must be provided with the definition. The type of a static member is not qualified by its class name.

Why must define static data member of class in cpp file?

As per OOPS guidelines compiler does not allocate memory to class instead of that it allocates memory to objects, but static members are independent of object, so to allocate memory to static variables, we define the static data members outside of the class, that's why once this variable is declared, it exists till the ...

Is a static data member it can only be initialized?

8.1: Static data. Any data member of a class can be declared static ; be it in the public or private section of the class interface. Such a data member is created and initialized only once, in contrast to non-static data members which are created again and again for each object of the class.


2 Answers

Why can't other static data members be initialized in the class definition? Was there a specific reason this was forbidden?

Most likely because C++ has separate translation units. The compiler needs to pick an object file where the initialization logic for those symbols will be placed. Forcing this to be in a specific source file makes that decision easy for the compiler.

If the data members are specific to the class, why are they declared at the global namespace scope and not some scope relevant to their class?

Because that's just how C++ does class members. This is no different than other class members like member functions:

Header file:

namespace example {

// Class declared in header
struct some_class
{
    // Member variable
    static float example;
    // Member function
    void DoStuff() const;
};

}

Source file:

namespace example {

    // Implement member variable
    float some_class::example = 3.14159;
    // Implement member function
    void some_class::DoStuff() const
    {
         //....
    }
}

There's a specific exception to allow static const integral members to be initialized in the header because it allows the compiler to treat them as compile-time constants. That is, you can use them to define sizes of arrays or other similar bits in the class definition.

like image 172
Billy ONeal Avatar answered Nov 14 '22 23:11

Billy ONeal


Why can't other static data members be initialized in the class definition? Was there a specific reason this was forbidden?

In general, all static objects require a definition, in one single translation unit, so that they have a well-defined address. As a special exception, static, constant, non-volatile class members don't need a definition if their address is not required, and they have a simple enough type that their value can be replaced by a compile-time constant.

Historically, "simple enough" was defined as an integral or enumeration type; C++11 extends that to include any literal type with a constexpr specifier.

If the data members are specific to the class, why are they declared at the global namespace scope and not some scope relevant to their class?

They are not declared at the global namespace scope. They are declared and scoped within the class.

If you mean, why are they defined outside the class definition, that's because there must be only one definition of the static member in the whole program; but the class must be defined in each translation unit that uses it.

like image 23
Mike Seymour Avatar answered Nov 14 '22 21:11

Mike Seymour