static variables in derived classes

Discussion in 'C++' started by Brad Kartchner, May 5, 2004.

  1. I'm attempting to write a program with several child classes derived from a
    single parent class. The parent class has a couple of variables that need
    to be present in each of the child classes. However, I need the objects
    created from each of the derived classes to share these variables with other
    objects of the same class but not with other objects of the other derived
    classes.

    Let me try to clarify with an example:
    //--------------------------------------------------------
    class A // parent class
    {
    protected:
    int Var1;
    int Var2;
    }

    class B : public A // derived class 1
    {
    }

    class C: public A // derived class 2
    {
    }

    B ObjectB1;
    B ObjectB2;
    C ObjectC1;
    C ObjectC2;
    //--------------------------------------------------------

    I would like ObjectB1 and ObjectB2 to share variable Var1 and Var2 with each
    other, but ObjectC1 and ObjectC2 should share a completely different Var1
    and Var2.

    Is there a way to do this?
     
    Brad Kartchner, May 5, 2004
    #1
    1. Advertising

  2. Brad Kartchner

    Ron Natalie Guest

    "Brad Kartchner" <> wrote in message news:A1cmc.119$...
    > I'm attempting to write a program with several child classes derived from a
    > single parent class.


    You need to define distinct static variables in each class to do this. If you want to deal
    with them generically in the base class, use a virtual function overloaded in each derived class
    to manipulate them (or I suppose you could use a template as well).
     
    Ron Natalie, May 5, 2004
    #2
    1. Advertising

  3. "Ron Natalie" <> wrote in message
    news:40994f68$0$18466$...

    > You need to define distinct static variables in each class to do this.

    If you want to deal
    > with them generically in the base class, use a virtual function overloaded

    in each derived class
    > to manipulate them (or I suppose you could use a template as well).


    Thanks for the response. I may have to use your overloaded function idea,
    even though I really don't want to. Well, it shouldn't be *that* hard.
     
    Brad Kartchner, May 5, 2004
    #3
  4. Brad Kartchner

    Siemel Naran Guest

    "Brad Kartchner" <> wrote in message news:A1cmc.119

    > I'm attempting to write a program with several child classes derived from

    a
    > single parent class. The parent class has a couple of variables that need
    > to be present in each of the child classes. However, I need the objects
    > created from each of the derived classes to share these variables with

    other
    > objects of the same class but not with other objects of the other derived
    > classes.


    Would recursive derivation help?

    template <class Derived>
    class A // parent class
    {
    protected:
    static int Var1;
    statoc int Var2;
    };

    class B : public A<B> // derived class 1
    {
    }

    class C: public A<C> // derived class 2
    {
    }


    template <class Derived> int A<Derived>::Var1;
    template <class Derived> int A<Derived>::Var2;
     
    Siemel Naran, May 6, 2004
    #4
  5. "Brad Kartchner" <> wrote in message news:<A1cmc.119$>...
    > I'm attempting to write a program with several child classes derived from a
    > single parent class. The parent class has a couple of variables that need
    > to be present in each of the child classes. However, I need the objects
    > created from each of the derived classes to share these variables with other
    > objects of the same class but not with other objects of the other derived
    > classes.


    class Base {
    template< typename Derived >
    int& Var1( Derived* derived_this /*TAD only*/ ) {
    static int var1; return var1;
    }
    };
    class Derived {
    Derived() { ++Base::Var1(this); }
    };

    Regards,
    Michiel Salters
     
    Michiel Salters, May 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Goudie
    Replies:
    6
    Views:
    473
    Victor Bazarov
    Jan 26, 2004
  2. Nils
    Replies:
    5
    Views:
    526
  3. Andrew Bullock

    static variables in derived classes

    Andrew Bullock, Apr 19, 2005, in forum: C++
    Replies:
    1
    Views:
    424
    Victor Bazarov
    Apr 19, 2005
  4. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page