0

I have a singleton class A, with virtual protected destructor and private constructor. I derived publicly from it and create class B.

Can i create two objects one each of base and derived class ?

If i cannot is there any other way to share same code ?

When i tried it i am getting compile time errors :

warning C4356: 'A::variable' : static data member cannot be initialized 
via derived class

B.cpp(4): error C2371: 'variable' : redefinition; different basic types
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
  • Theoretically you can, if the base class has a protected constructor. – BartoszKP Oct 08 '13 at 10:11
  • 11
    Don’t use singletons and you won’t have problems related to singletons. –  Oct 08 '13 at 10:12
  • Why not try it yourself and find out? – Rohit Vipin Mathews Oct 08 '13 at 10:13
  • Don't write software and you won't have problems related to software engineering. Sound logic. :P – Tony The Lion Oct 08 '13 at 10:13
  • @TonyTheLion Except that singletons are a rather problematic pattern (it's just a thinly veiled global variable, after all), so avoiding singletons generally leads to fewer problems and better design. I understand singleton utility when dealing with legacy code, but I try hard to avoid the (anti-)pattern in any new code I write. – Angew is no longer proud of SO Oct 08 '13 at 10:16
  • http://stackoverflow.com/questions/137975 (although that just describes language-independent conceptual problems; C++ adds a whole pile of lifetime-management issues to the mix). – Mike Seymour Oct 08 '13 at 10:20
  • @TonyTheLion: Sometimes we need to write software, so we have to deal with some problems. There's no need to create more problems than necessary when we do that. – Mike Seymour Oct 08 '13 at 10:30

3 Answers3

0

It all depends on how your singleton is implemented. If it has a public constructor of some sort than you're probably fine with extending it's singleton-ness to a derived instance of B. If it's some kind of factory method then probably not. The fact that it has a protected dtor is a sign that it was built for use as a base class.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

Don't be confused with two independent idioms:

  • Inheritance is a part of C++ language mechanics
  • Singleton is an instantiation policy, defined in your code

So the answer to your question is "it depends on how you've implemented the instantiation policy".

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
0

If I understand right, what you want to achieve is a reusable singleton pattern base class. You can do this using a template base class, using a pattern named CRTP. This question also provides a simple explanation of this technique.

Concretely, it would mean doing something like this:

template<typename TDerived>
class Singleton {
public:
    static TDerived& instance() {
         static TDerived instance_;
         return instance_;
    }
protected:
    Singleton() {}
    virtual ~Singleton() {}
};

class ConcreteSingleton : public Singleton<ConcreteSingleton> {
public:
    ...
protected:
    ConcreteSingleton() {
         ...
    }
    virtual ~ConcreteSingleton() {
         ...
    }

};

This particular version requires that your base classes have a default constructor. Other, more complex interfaces can alleviate that issue.

Overall, as pointed out in some comments, singleton as a pattern has scalability issues, though const singletons are generally considered safer.

Community
  • 1
  • 1
Martin J.
  • 5,028
  • 4
  • 24
  • 41