• Advertisement
Sign in to follow this  

[C++] Static Members

This topic is 4498 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

If I were to declare a static member in a base class, would the derived class also inherit this?
class Base
{
protected:
    static int StaticVar;
};

class Derived : public Base {};

In the above example, is Base::StaticVar the same variable as Derived:StaticVar, or are they different? I'm guessing the same, since a static member is just a global with class scope.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by _goat
Best thread ever.


Well, it is one of the reasons why you can't create a 'Singleton' base class from which to inherit all singletons.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Well, it is one of the reasons why you can't create a 'Singleton' base class from which to inherit all singletons.


Unless it's a template class that uses the curiously recurring template pattern or something similar.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by ToohrVyk
Quote:
Original post by _goat
Best thread ever.


Well, it is one of the reasons why you can't create a 'Singleton' base class from which to inherit all singletons.


Huh?

C++

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by ToohrVyk
Well, it is one of the reasons why you can't create a 'Singleton' base class from which to inherit all singletons.


Unless it's a template class that uses the curiously recurring template pattern or something similar.


At which point it's not one base class, because you cannot cast all singletons in your program down to that class (it would not even make sense, since each singleton is based on a specific instance of the template). My point was precisely that you need to add the curiously recurring template pattern, or something similar, on top of plain and simple inheritance, to achieve this.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by _goat
Best thread ever.


Well, it is one of the reasons why you can't create a 'Singleton' base class from which to inherit all singletons.


i did that, although i dont use it any more


/*
this is a utility class to
allow a single destruction point
it basically makes every singleton easily destroy()ed in one call
*/

class SingletonInterface
{
private:
static vector<SingletonInterface*> singletons;
protected:
/*
this is overloaded so it will work on any derivative
TODO: check if this is necessary
also adding this makes the class abstract
*/

virtual void destroy() = 0;
public:
SingletonInterface()
{
singletons.push_back( this );
}
/*
declared virtual so singleton destrucion works
*/

virtual ~SingletonInterface(){}

/*
will dispose of all singletons in one go
after calling this, all calls to
getInstance() will segfault (at best)
*/

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

/*
this is a class to ensure there
s only ever a SINGLE INSTANCE and
a centralised access point to the
class
*/

template< class Type >
class Singleton : public SingletonInterface
{
private:
static Type *instance;
protected:
/*
ensures singleton isnt active
*/

Singleton()
{
assert( !instance );
instance = (Type*)this;
}

/*
removes this as the current instance
*/

~Singleton()
{
instance = NULL;
}

/*
removes this as the current instance
*/

virtual void destroy()
{
delete instance;
instance = NULL;
}
public:
/*
returns a reference to the current instatiation
*/

static Type& getInstance()
{
assert( instance != NULL );
return *instance;
}

/*
returns a pointer to the current instatiation
*/

static Type* getPointer()
{
assert( instance != NULL );
return instance;
}

};

/*
static pointer to the single instance that exists
*/

template<class Type>
Type *Singleton<Type>::instance = NULL;


Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by ToohrVyk
Well, it is one of the reasons why you can't create a 'Singleton' base class from which to inherit all singletons.


i did that, although i dont use it any more


Exactly: you create singletons by instantiating a template, instead of making the class of your singleton inherit from a given class.

To clarify what I meant: I say that you cannot write a class "BaseSingleton" which allows the code below to create two singletons Alpha and Beta:


class Alpha : public BaseSingleton {
public: void Frobnicate( ) { cout << "Alpha!" << endl; }
};

class Beta : public BaseSingleton {
public: void Baznicate( ) { cout << "Beta!" << endl; }
};


Making Alpha and Beta be independent singletons will require more than inheritance, namely a template, preprocessor magic, or adding singleton management directly to the classes.

Share this post


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

  • Advertisement