Inheritance & Templates

M

Michael

Ok,
I've got a Singleton template:

template<class T>
class Singleton
{
public:
static T& GetInstance()
{
static T* pInst= NULL;
if(pInst == NULL) pInst = new T;
return &pInst;
}


};

Then to use it, I do the following:

class FileSystem: public Singleton<FileSystem>
{
/*
 
R

Rolf Magnus

Michael said:
Ok,
I've got a Singleton template:

template<class T>
class Singleton
{
public:
static T& GetInstance()
{
static T* pInst= NULL;
if(pInst == NULL) pInst = new T;

No need for the if(), you can just do:

static T* pInst = new T;

The new will be executed exactly once - the first time GetInstance is
called.
return &pInst;
^
I guess you mean * here, not &.

Btw, note that the object will never be destroyed correctly if you write
your singleton like that. The memory will be freed on program exit on any
decent system, but destructors will not be called.
Then to use it, I do the following:

class FileSystem: public Singleton<FileSystem>
{
/*
.
.
.
*/
void FSfunc1();
};

int main()
{

FileSystem::GetInstance().FSfunc1();

}


Now if I want to create a vector of pointers to the Singletons, how do i
do that? They all 'sort-of' have the same base type, or do i need to
derive Singleton from a non-templated class and store pointers to that??

Yes. Your template instances are not related in the OO sense.
I want my program to be pluggable, loading dll's then dll's in different
modules to be able to interact with each-other but passsing
round-identifier strings, hence I can't make explicit calls!

Then I'd not use a vector, but a map, which gives you faster name lookup.
 
M

Michael

Rolf Magnus said:
No need for the if(), you can just do:

static T* pInst = new T;

The new will be executed exactly once - the first time GetInstance is
called.

^
I guess you mean * here, not &.


Btw, note that the object will never be destroyed correctly if you write
your singleton like that. The memory will be freed on program exit on any
decent system, but destructors will not be called.

** SEE Comment below **
Yes. Your template instances are not related in the OO sense.


Then I'd not use a vector, but a map, which gives you faster name lookup.


I've not yet quite got the hang of the STL!
if I make the destructors virtual in the base classes, then can i not call
delete on all the registered pluggins to shut them down correctly??
Thanks,
Regards,

Mike
 
R

Rolf Magnus

Michael said:
I've not yet quite got the hang of the STL!

A map is an associative container. You could use it something like this:

// define your map
std::map<std::string, MyBaseClass*> mymap;

// fill it
mymap["whatever"] = new MyDerivedClass;

// get an entry
MyBaseClass* obj = mymap["whatever"];

As you can see, it's pretty straight forward. And std::map uses a binary
tree search which is usually quicker than the linear search you would have
to do with a vector.
if I make the destructors virtual in the base classes, then can i not
call delete on all the registered pluggins to shut them down correctly??

Yes, of course you can. I just meant that they will not get destroyed
automatically. You have to explicitly destroy them, and of course ensure
that you don't attempt to use them afterwards.
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top