Template class and inheritance

M

Mr. Croup

Hi, maybe someone can help with this one...

I have a templated singleton class from which some manager derives

template<class S> class Singleton
{
private:
static S * SingletonInstance;
public:
static S* Instance()
{
if (SingletonInstance == NULL)
{
SingletonInstance = new S();
}

return SingletonInstance;
}
};

Let's say I have one AbstractManager that derives from this singleton,
and a ConcreteManager that derives from AbstractManager. The only way
I found to make this work is having the AbstractManager be a template
class, like this :

template<class R> class AbstractManager: public Singleton<R>
{
friend class Singleton<R>;
}

and

class ConcreteManager : public AbstractManager<OpenGLRenderer>
{
friend class Singleton<OpenGLRenderer>;
}

Which is unfortunate because I can't declare non abstract virtual
methods in AbstractManager. I can't also have pointers to the base
AbstractManager class on the ConcreteClass. For example, with code
like this :

Core::AbstractManager<ConcreteManager> * Mgr =
Core::ConcreteManager::Instance();
Mgr->MethodDeclaredInAbstractManager();

The compiler complains with :

unresolved external symbol "public: void __thiscall
AbstractManager<class
ConcreteManager>::MethodDeclaredInAbstractManager()"

Even if the method was declared and implemented in AbstractManager.

So, how should I handle multi-level inheritance from a classe that is
templated?

Thanks! :)
Simon
 
A

Aman Angrish

Mr. Croup said:
Hi, maybe someone can help with this one...

I have a templated singleton class from which some manager derives

template<class S> class Singleton
{
private:
static S * SingletonInstance; ....
template<class R> class AbstractManager: public Singleton<R>
{

....
class ConcreteManager : public AbstractManager<OpenGLRenderer>
{
Core::AbstractManager<ConcreteManager> * Mgr =
Core::ConcreteManager::Instance();

ConcreteManager::Instance() would give you OpenGLRenderer* .
(the pointer to the type of the template parameter.)

Are you sure you want to do this "AbstractManager<ConcreteManager> " ?
i.e. BaseClass<DerivedClass>
If you do, you might want to look at the "Curiously Recurring Template
Pattern" (CRTP) and design your base class accordingly.
I've found it to be useful on many occassions but make sure that is the
class design YOU really need.

regards,
Aman Angrish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top