473,586 Members | 2,707 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 * SingletonInstan ce;
public:
static S* Instance()
{
if (SingletonInsta nce == NULL)
{
SingletonInstan ce = new S();
}

return SingletonInstan ce;
}
};

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<OpenG LRenderer>;
}

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::AbstractM anager<Concrete Manager> * Mgr =
Core::ConcreteM anager::Instanc e();
Mgr->MethodDeclared InAbstractManag er();

The compiler complains with :

unresolved external symbol "public: void __thiscall
AbstractManager <class
ConcreteManager >::MethodDeclar edInAbstractMan ager()"

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 2585
"Mr. Croup" <si***********@ gmail.com> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.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 * SingletonInstan ce; .... template<class R> class AbstractManager : public Singleton<R>
{
.... class ConcreteManager : public AbstractManager <OpenGLRenderer >
{ Core::AbstractM anager<Concrete Manager> * Mgr =
Core::ConcreteM anager::Instanc e();


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

Are you sure you want to do this "AbstractManage r<ConcreteManag er> " ?
i.e. BaseClass<Deriv edClass>
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
2194
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 fine till I have to create a Stack object - all sorts of error messages are generated. I have included source files below: The List class: ...
13
2810
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 { }; template <typename Base> class Tpl : protected Base {
3
2665
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... Given: #include <string> // Parent class
3
2989
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: class A{ }; class B: public A{}; And for the constructor of B: B:B():A(){} now suppose I have a template as:
2
1947
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 a template class from a base class that is also a template class. It's valid to derive a template class from a concrete base class.
1
1555
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 following. In this case is the derived class a class template and the base class a class template. Both the base class and the derived class will be...
5
2076
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 works fine but there in one thing that I'm unsure about and that is the inheritance statement. What difference is it if I have this construction...
2
2480
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: VertexValueTyp& vertex(int i); that dereferences vertices internally in case VertexTyp is a pointer
9
3458
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. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
32
2708
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 issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8216
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.