Inheriting from Template Class

Hey everyone,

While designing a Singleton class for an app I'm creating, I looked at the Ogre implementation of a Singleton (motivation and usage details here:http://www.ogre3d.org/wiki/index.php/Singleton) and created a template-based system. In order to make a class a Singleton, you simply inherit the Singleton class, like so:

1
2
3
4
class OnlyOne : public Singleton<OnlyOne>
{
...
}


The problem is that my compiler won't allow this sort of (semi) self-referential template nonsense, seeing as the OnlyOne class hasn't been properly declared. I tried forward declaration, but that didn't work either. Any other suggestions? Using MinGW 3.4.5

EDIT: topic name edited to better reflect the problem
Last edited on
That should work just fine. You just can't use an object of the derived type in the template. You have to use a pointer or a reference. I didn't even need to forward declare it:

1
2
3
4
5
6
7
8
9
10
11
12
template <typename T>
class Singleton
{
public:
  static T& Instance();  // note: reference
};

//----------------------

class OnlyOne : public Singleton<OnlyOne>  // works just fine
{
};
Strange...I'm still getting problems with it, despite the use of a reference. Which compiler are you using?
Ah, wait, I think I got it. Thanks for your help Disch.
Turns out it was all due to unrelated namespace errors. Thanks for your time!
Topic archived. No new replies allowed.