2

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

7
  • 1
    SideNote: First line of defense for singleton-logic. Ask yourself wtf am I doing using a singleton? Commented Apr 16, 2013 at 12:19
  • 2
    "I meet problem when I use Singleton" Yes, that's what usually happens. Commented Apr 16, 2013 at 12:23
  • Hi WhozCraig, In fact, I'm a Java guy. I'm a newbie in C++, so my knowledge in C++ is very little. Sorry for my silly question :) Commented Apr 16, 2013 at 12:25
  • 1
    @HienNguyen: In Java, singletons merely increase coupling, hide dependencies, and make (unit) testing unnecessarily difficult. In C++, they do all that, and also introduce horrendous lifetime-management issues; it's much better to avoid them. Commented Apr 16, 2013 at 12:27
  • 1
    Hi Peter Wood, I'm reading it. Thanks for your support Commented Apr 16, 2013 at 13:09

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi Angew, Thanks for your concern and your help. I'm a newbie in C++, so I have some silly questions :(. Will the application create many static_instance or only one, if the OuterClass A calls GetInstance of InnerClass InnerA many times ?
Hi Angew, after debugging, I see it only creates one not many static_instance. Thanks for your help :) Have a nice day
Hi Angew, in your code will C++ delete static_instance automatically after finish the app ? Please help me one more time! Thanks for that :)
@HienNguyen When a function-local variable is declared as static, it gets static storage duration (the same as global variables get). This means it's destroyed at program termination, assuming it was ever initialised. If control never entered the function, the var was never initialised and will thus not be destroyed.
@HienNguyen And as you've already figured out, static variables of function scope are only ever initialised once. This applies even in the presence of multiple threads (at least since C++11).
|
1

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.

3 Comments

Hi Matthieu, Thanks for your explanation and the related link, it's very helpful. However, I'm confused about Implementation 2, the variable S will be destroyed automatically by C++, won't it ?
Angew explained it for me in above comment, thanks for your help and sorry for not voting for your answer because I don't have enough reputation.
@HienNguyen: Yes, that is the beauty of the implementation actually. The standard guarantees that all "globals" that were dynamically initialized will be destroyed in the reverse order in which they were created. Automatically. You can emulate this with Implementation 1 by using atexit to register a callback to be called during shutdown; it's the underlying mechanism if you do not want fancy things :)
0

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

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.