Click to See Complete Forum and Search --> : inheritance and static data


dave2k
June 15th, 2007, 10:37 AM
i need a static bool in each of my derived classes. obviously i cannot stick it in the base class because each of the derived classes needs it's own static bool. the only option i can see is declaring the static bool in each of the derived classes, but this seems to go against being OO.. Any other options?

at the moment i am doingclass Base
{
};

class Derived1 : public Base
{
public:
static bool m_b;
};

bool Derived1::m_b = false;

class Derived2 : public Base
{
public:
static bool m_b;
};

bool Derived2::m_b = false;

int _tmain(int argc, _TCHAR* argv[])
{
Derived1 d1;
Derived2 d2;

Derived1::m_b=true;
Derived2::m_b=false;


return 0;
}

exterminator
June 15th, 2007, 11:45 AM
Does this work:
struct Base{};
template<typename T>
struct BoolAdder : Base
{ protected: static bool m_bool;};
template<typename T>
bool BoolAdder<T>::m_bool = false;
struct Derived : BoolAdder<Derived> {};
struct AnotherDerived : BoolAdder<AnotherDerived> {};

Graham
June 15th, 2007, 01:10 PM
... but this seems to go against being OO.. ...
Why? You're declaring a necessary member at the point it's needed. I would have said that trying to move it up a level would be against OO principles, given the criteria you've stated.

code_carnage
June 16th, 2007, 01:00 AM
But why keeping the static in BASE class is against the rule of OO..??

I mean code reuse is also an aim for OO programming....

WHy unnessessary declare STATIC variable in all the derived classes If all the derived classes has to have the variable..

Why should not we keep that in BASE class only...???

Regards
Prashant

sinall
June 16th, 2007, 02:18 AM
Does this work:
struct Base{};
template<typename T>
struct BoolAdder : Base
{ protected: static bool m_bool;};
template<typename T>
bool BoolAdder<T>::m_bool = false;
struct Derived : BoolAdder<Derived> {};
struct AnotherDerived : BoolAdder<AnotherDerived> {};
I support this method. Maybe this is the only method to solve the problem.

exterminator
June 16th, 2007, 04:30 AM
Why should not we keep that in BASE class only...???Probably because it is not an attribute of the base class?

Going back to what Graham said, which is always meaningful. You might want to think about it. Introducing the intermediate class does solve the problem but it unnecessarily adds up to the hierarchy. What you could rather do is make the static member public in the BoolAdder and make the derived class inherit from it privately (also remove the coupling between it and the base class). That will hide out the inheritance relationship from the outside world and the further derived class (if just outside world then protected inheritance may help).

The private/protected inheritance relationship is "implemented in terms of" and it in this case is similar to what composition would have achieved. So, necessarily it is just composition that you want but if you strictly not intend to add the member into derived classes explicitly - the non-public inheritance works as well.

miteshpandey
June 16th, 2007, 07:48 AM
Nice solution Exterminator :thumb:

From Exterminator's solution I would like to inference one thing.

If variables (except pointers) of same type but meant for different functionality is needed at each hierarchy level then then this template solution can be applied as a generic solution to the problem.

miteshpandey
June 16th, 2007, 07:58 AM
But one thing I am not sure about is deriving BoolAdder from Base or use Multiple inheritance

Graham
June 16th, 2007, 08:24 AM
But why keeping the static in BASE class is against the rule of OO..??

I mean code reuse is also an aim for OO programming....

WHy unnessessary declare STATIC variable in all the derived classes If all the derived classes has to have the variable..

Why should not we keep that in BASE class only...???

Regards
Prashant

Code re-use in OO does not mean deriving from a class simply because that class has some code you want to use. Code re-use means re-using code that's written in terms of the base class by supplying it with different derived classes.

As Sutter and Alexandrescu put it: inherit in order to be reused (by code written in terms of the base class), not to reuse base class code.

There's a subtle difference between this and exterminator's example, which is factoring out common code - the emphasis is different.

What it really boils down to is: does the variable have meaning as an attribute of the base class? If not, then it has no business being there. In the OP's example, there was also the factor that each derived class needed a separate copy of the variable, so puttng it in the baseis a non-starter, anyway.

exterminator
June 16th, 2007, 10:58 AM
But one thing I am not sure about is deriving BoolAdder from Base or use Multiple inheritanceMI... but it is an MI that is not known to user code as per my suggestion above. Like this:struct Base{};

template<typename T>
struct BoolAdder
{
protected:
static bool m_bool;
};

template<typename T>
bool BoolAdder<T>::m_bool = false;

struct Derived : public Base, private BoolAdder<Derived> {};
struct AnotherDerived : public Base, private BoolAdder<AnotherDerived> {};This looks much better. But really it is no diferent than composition of the static member. It just saves explicit adding of the static member in each derived classes. And would probably save some typing if the BoolAdder were a little bulkier. Nothing more.