Jump to content
  • Advertisement
Sign in to follow this  
PlayfulPuppy

Template-static member variable?

This topic is 4917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, this is a wierd question (I think): Is there any way to make a static member variable inside a class that is static to ALL classes derived from that template? That sounds awkward, so I'll explain what I'm trying to do: I've got a templated Singleton class, and any class that wishes to use it declares:
class MySingleton : public CSingleton<MySingleton>
Fairly standard to the best of my knowledge (Although if there are better ways I'd like to hear of them, I dont like the concept of a base class being templated). The thing is, I want to be able to keep a list of pointers to all intiailised Singletons, so during shutdown it can just iterate through and call CSingleton::DestroySingleton() to avoid any accidental memory leaks (Although it would produce a warning in the logs so it can be fixed quick-smart). Thing is, though, I'm trying to be neat about this and I'd like this list of pointers to be a member of my Singleton class rather than some global flying about or creating a manager class for this express purpose (Which seems fairly overkill for the small chance someone might forget a DestroySingleton). And, naturally, this member would need to be shared between ALL derivatives of the templated class. So is there a way to define a static member variable that applies to all derived classes from the template? Or is my Singleton class ass backwards from sunday?

Share this post


Link to post
Share on other sites
Advertisement
I'm not 100% positive but my guess is that you can't have a static var shared between different the types that use the template. What you could do though is have some class called CSingletonBase or something which CSingleton inherits. Then CSingletonBase can hold the static list of pointers and the cleanup function.

Share this post


Link to post
Share on other sites
What you want to do is maintain a static list of all the Singletons that you create, then you can register a function with the atexit() function (this function is called by the C Runtime Library before the application exits) which ensures that all of the created singletons are destroyed.

Depending on your exact design something like this :


static std::vector<Singleton*> instances;

void destroySingletons()
{
for(int i=0;i<instances.size;i++)
{
instances->destroy;
}
}

atexit(destroySingletons);




You just have to be cautious about exactly where you store you static vector of singletons, should it be a global? within some other singleton class? you also have to make sure that whenever a singleton is created or destroyed it adds or removes itself from the list. I hope that this has given you some ideas, maybe if you do a bit of googling around Singletons and the atexit function

Share this post


Link to post
Share on other sites
You could also store a list of smart pointers (e.g. boost's shared_ptr) instead of bare pointers. Then they should be cleaned up automatically.

Hope this helps,
Pete

Share this post


Link to post
Share on other sites
Quote:
Original post by moagstar
What you want to do is maintain a static list of all the Singletons that you create, then you can register a function with the atexit() function (this function is called by the C Runtime Library before the application exits) which ensures that all of the created singletons are destroyed.


The problem with this is that Singleton is templated so you can't have a container holding each of the different templated types. Stro is thinking along the right sort of lines:



class SingletonBase
{
public:
SingletonBase() { singletons.push_back(this); }
virtual ~SingletonBase();
virtual void Cleanup() = 0;
private:
static std::vector<SingletonBase *> singletons;
};




If you ever manually destroy singletons before the end of your program then SingletonBase's destructor will have to remove it's this pointer from the static singleton array.

At the end of your program you'll simply run through the singletons vector and call Cleanup on each class.

James

Share this post


Link to post
Share on other sites
Hello PlayfulPuppy,

Try a static reference in the template that is set to a gobal/file scope variable.

That should do it if the compiler accepts it.

This way each verison of the template shares the gobal/file scop variable though each template static reference.

Lord Bart :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!