Archived

This topic is now archived and is closed to further replies.

supagu

static classes?

Recommended Posts

im currently working on my engine, and its supposed to be all OO, but i have made some if it not OO by using "namespace" to group realated code that i want static, is there a better way of going about this? what i am trying to achive is a static class, but other static classes can be inherited from these static classes so that they may be changed for specific apps. for example i have a "RenderManager" class, i only want to allow 1 global version of it, so i made it using namespace, but then i cant extend it to allow for change hope you understand what im getting at, and any solutions to my problem thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Take a look at Singletons - a design pattern to give you global access to an object without making it global...kinda
CYer, blitz

Share this post


Link to post
Share on other sites
quote:
Original post by supagu
for example i have a "RenderManager" class, i only want to allow 1 global version of it, so i made it using namespace, but then i cant extend it to allow for change



whats the problem with just making the constructor private?

Share this post


Link to post
Share on other sites
That''s the Singleton pattern. A simple example:


class Singleton
{
public:
~Singleton();

Singleton& getInstance()
{
static Singleton instance( args );
return instance;
}

protected: // c''tor can be private or protected
Singleton( args );
}


The choice of making the constructor private or protected depends on usage. Also, some classes may have serious construction overhead and you would not want the class constructed on the first call to getInstance. In this case, you could make a private static member of Singleton and set it up to be initializes at startup time. Lots of info available on the net regarding Singletons.

Share this post


Link to post
Share on other sites
Actually, protected constructors are pretty sweet. Basically, you can''t access the class directly, only another class that is inherited from it can access its constructor(or that class itself of course). Good to use for something like a base for other classes in the same type of group.

Namespaces do make code object oriented. Classes, Structs, Namespaces, Enums all come from the same family. C# which is completely OOP, relies heavily on namespaces. I see no problem with just using namespaces if you want to encapsulate functions. Or you could always make some static methods in a base class.

Share this post


Link to post
Share on other sites