Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static variables in derived classes

Reply
Thread Tools

static variables in derived classes

 
 
Brad Kartchner
Guest
Posts: n/a
 
      05-05-2004
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?


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      05-05-2004

"Brad Kartchner" <(E-Mail Removed)> wrote in message news:A1cmc.119$(E-Mail Removed)...
> 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).

 
Reply With Quote
 
 
 
 
Brad Kartchner
Guest
Posts: n/a
 
      05-05-2004

"Ron Natalie" <(E-Mail Removed)> wrote in message
news:40994f68$0$18466$(E-Mail Removed) m...

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


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      05-06-2004
"Brad Kartchner" <(E-Mail Removed)> 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;



 
Reply With Quote
 
Michiel Salters
Guest
Posts: n/a
 
      05-06-2004
"Brad Kartchner" <(E-Mail Removed)> wrote in message news:<A1cmc.119$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
static variables in derived classes Andrew Bullock C++ 1 04-19-2005 02:27 PM
static variables in derived classes Nils C++ 5 02-24-2005 11:12 AM
Base Classes in .exe, derived classes in .dll Colin Goudie C++ 6 01-26-2004 03:18 PM



Advertisments