|
-
June 15th, 2007, 10:37 AM
#1
inheritance and static data
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 doing
Code:
class 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;
}
-
June 15th, 2007, 11:45 AM
#2
Re: inheritance and static data
Does this work:
Code:
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> {};
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
June 15th, 2007, 01:10 PM
#3
Re: inheritance and static data
 Originally Posted by dave2k
... 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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 16th, 2007, 01:00 AM
#4
Re: inheritance and static data
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
-
June 16th, 2007, 02:18 AM
#5
Re: inheritance and static data
 Originally Posted by exterminator
Does this work:
Code:
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.
-
June 16th, 2007, 04:30 AM
#6
Re: inheritance and static data
 Originally Posted by code_carnage
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
June 16th, 2007, 07:48 AM
#7
Re: inheritance and static data
Nice solution Exterminator
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.
If there is no love sun won't shine
-
June 16th, 2007, 07:58 AM
#8
Re: inheritance and static data
But one thing I am not sure about is deriving BoolAdder from Base or use Multiple inheritance
If there is no love sun won't shine
-
June 16th, 2007, 08:24 AM
#9
Re: inheritance and static data
 Originally Posted by code_carnage
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.
Last edited by Graham; June 16th, 2007 at 08:27 AM.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 16th, 2007, 10:58 AM
#10
Re: inheritance and static data
 Originally Posted by miteshpandey
But one thing I am not sure about is deriving BoolAdder from Base or use Multiple inheritance
MI... but it is an MI that is not known to user code as per my suggestion above. Like this:
Code:
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|