What if a static member is not initialized and the member type is class itself?

Patrick

A class is defined like this:

class Singleton {
public:
    static Singleton instance_;

private:
    Singleton() {
        cout << "constructor\n";
    }
};

In a function:

Singleton instance = Singleton::instance_;

Code can be compiled and no error was thrown. If I use it like this:

Singleton &instance = Singleton::instance_;

A link error was thrown. I wonder why the first case can be compiled correctly? And I know the constructor function was not called. What's the object state of instance in the first case? Does the first case make any sense?

user743382

Both forms are an error in your code, but in both cases compilers/linkers are allowed to silently ignore the error, they're not required to detect it.

Singleton instance = Singleton::instance_; uses the implicitly generated copy constructor. It copies all 0 non-static data members from Singleton::instance_, and is therefore likely to be optimised away entirely. Therefore, the fact that you're using Singleton::instance_ without a definition is likely to go unnoticed.

Singleton &instance = Singleton::instance_; binds to Singleton::instance_ and requires its address to be known. Therefore, it is more likely to result in an error if Singleton::instance_ is not defined.

The way you can provide a definition of Singleton::instance_ is

Singleton Singleton::instance_;

at file scope.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is it allowed to have a class that has an incomplete type static member of itself?

Class Template Static Member Initialized Twice

Static member has to be initialized?

Are static class variables initialized before first call of static member function?

The Standard seems to support (the snippet below compiles) a static data member having the same type as the class itself

Static constexpr data member initialized off-class, and a possible workaround?

Initialization order guarantees for inline-initialized static const class member

Why can a "const static int" member be initialized within a class, but not a "const static other" member?

Initialize static const member of a class, where the member is of a private type?

Can't a class have static constexpr member instances of itself?

How to generate a bool Member Type via a static member New directly on the Measure type itself?

Why static global variables initialized to zero, but static member variable in class not initialized?

Can I use a member variable of type ofstream initialized in the class constructor?

C# declare class member variable type of itself pointer

Accessing static member class

Initializing a static class member

Check if class member is static

Static enum class member

Static member in a template class

Keword this - a class member reffering to itself

Should there be any need to type 'static' before each member in a static class?

Is a static member initialized before the constructor is called?

deallocation order of the static member and detached thread itself

uninit_member: Non-static class member m_wszParams is not initialized in this constructor nor in any functions that it calls in C++

Is a declaration for a non-static data member of a class type also a definition?

static_assert type of a private class member, with no instance

Why does constexpr static member (of type class) require a definition?

static class template member: invalid application of ‘sizeof’ to incomplete type

In-class initialization from static member of the same type