Hien Nguyen
Hien Nguyen

Reputation: 764

Use Singleton in Inner Class in C++

Thanks for everyone because of helping me ^^ my problem is resolved !

I meet problem when I use Singleton in inner class using C++ language. Please help me and sorry for my bad English.

class A: public AA
{
    class InnerA
    {
        private:
            static InnerA* _innerA;
        public:
            ~InnerA();
            void Release();

            static InnerA* GetInstance()
            {
                if (_innerA == NULL)
                {
                    static InnerA static_instance;
                    _innerA = &static_instance;
                }
                return _innerA ;
            };
.....

The console show the below error after compiling :

undefined reference to `A::InnerA::_innerA'

I also modify like below code, but I still meet the above error.

class A: public AA
    {
        class InnerA
        {
            private:
                static InnerA* _innerA;
            public:
                ~InnerA();
                void Release();

                static InnerA* GetInstance()
                {
                    if (A::InnerA::_innerA == NULL)
                    {
                        static A::InnerA::_innerA static_instance;
                        _A::InnerA::_innerA = &static_instance;
                    }
                    return A::InnerA::_innerA;
                };
    .....

Please help me !!! Thanks very much

Upvotes: 2

Views: 1155

Answers (4)

Matthieu M.
Matthieu M.

Reputation: 300359

First things first: you are conflating two different implementations of the Singleton pattern.

Implementation 1

The trusty leaky Singleton.

// Singleton.hpp
class Singleton {
public:
    static Singleton& Instance() {
        if (I == nullptr) { I = new Singleton(); }
        return *I;
    }

private:
    Singleton();
    ~Singleton();

    static Singleton* I;
};

// Singleton.cpp
Singleton* Singleton::I = 0;

Two issues:

  • leaks, unless you implement a Release and make sure to call it (once)
  • not thread safe

Implementation 2

Known as Meyers' Singleton.

// Singleton.hpp
class Singleton {
public:
    static Singleton& Instance() {
        static Singleton S;
        return S;
    }

private:
    Singleton();
    ~Singleton();
};

Main issue:

You should pick either, but not mix the two.

Upvotes: 1

ogni42
ogni42

Reputation: 1276

In you class A implementation file you have to write something like\

A::InnerA *A::InnerA::_innerA;

to complete the definition of the member. In the header you only declare the member, but you don't define it.

Upvotes: 0

4pie0
4pie0

Reputation: 29754

you have always define static data member. put this before main, just under your class A definition:

A::InnerA *A::InnerA::_innerA;

Upvotes: 0

All static data members of a class must be defined somewhere. Put the following into a .cpp file:

A::InnerA *A::InnerA::_innerA;

However, do you actually need that static pointer at all? You could just simplify your code like this:

class A: public AA
{
    class InnerA
    {
        public:
            ~InnerA();
            void Release();

            static InnerA* GetInstance()
            {
                static InnerA static_instance;
                return &static_instance;
            }
.....

Then, you wouldn't need any static member definitions.

Upvotes: 2

Related Questions