473,509 Members | 3,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template class and inheritance

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

May 14 '06 #1
1 2578
"Mr. Croup" <si***********@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com
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.
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
May 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2188
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
13
2797
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
2658
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
3
2976
by: Gandu | last post by:
Could some C++ guru please help me. I have a template based general linked list class that I want to inherit publicly to create a queue class. In the case of non-template inheritance, I can use:...
2
1938
by: Tony Johansson | last post by:
Hello Experts To derive a concrete class from a template class in invalid. Why?? So you should not be able have something like this class Derived : public Base<int, 100> It's valid to derive...
1
1554
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
5
2073
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
2
2473
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
9
3446
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
32
2696
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.