static variables in derived classes

B

Brad Kartchner

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?
 
R

Ron Natalie

Brad Kartchner said:
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).
 
B

Brad Kartchner

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

Siemel Naran

Brad Kartchner said:
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;
 
M

Michiel Salters

Brad Kartchner said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top