CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    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;
    }

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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> {};

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: inheritance and static data

    Quote 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


  4. #4
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    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

  5. #5
    Join Date
    Nov 2004
    Posts
    13

    Re: inheritance and static data

    Quote 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.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: inheritance and static data

    Quote 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.

  7. #7
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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

  8. #8
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: inheritance and static data

    Quote 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


  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: inheritance and static data

    Quote 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured