Click to See Complete Forum and Search --> : Static variables + polymorphism?


scellers
June 26th, 2008, 12:24 PM
Hi,
So I'm trying to write a program where there is a base class (called Segment) which implements a few basic functions that all its child classes (PacketHeader, etc.) will use.
The problem is, PacketHeader and other classes like it have class variables: that is, variables that are the same for each instance of the class, so static. The functions implemented by Segment should use the values for the class calling instead of the ones defined for Segment. Is there any way to do this?

Here's some sample code:

class Segment
{
protected:
char voidchar;
void *ptrarray[];
static char *fieldname[];
static char type[];
public:
Segment();
int getType(int index);
char* getFieldname(int index);

virtual int writeField(int index, void *buffer);
virtual int dump(char *delimiter);

static int totalfields;
};

class PacketHeader : public Segment
{
protected:
void *ptrarray[10];
static char *fieldname[10];
static char type[10];
public:
PacketHeader();

static int totalfields;
char versionID[2];
unsigned int size;
char date[10];
};

(Quick note about the above code: all the scope, virtuals, etc. is very temporary as I'm trying to get this to work.)

So to clarify, is it possible to make it so when a PacketHeader object calls the writeField or dump functions, those functions use the PacketHeader's version of fieldname and type?

Thanks a lot!

Edit: When I take out the re-declarations of the static variables from the derived class, trying to initialize the fieldname, type fields leads to this error:
filename.cpp:175: error: ISO C++ does not permit 'Segment::fieldname' to be defined as 'PacketHeader::fieldname'
I'm guessing this is my real problem. Is there another solution except to make those variables not static?
Thanks again.

MrViggy
June 26th, 2008, 02:39 PM
I'm not sure I understand. You have static arrays in the base class, and you want to have the derived classes determine their sizes? If so, just use a vector, and resize it in the constructor of the derived class.

Viggy

TheCPUWizard
June 26th, 2008, 03:58 PM
I agree with MrViggy based on the way the situation is explained, but the concept of class level data that is polymorphic is a common and viable one.

The solution is actually VERY simple.

static private data that is NEVER directly accessed except by virtual accessor methods. Thus you get one copy of the data that is shared by all instances of the class, yet the values can be treated polymorhpically.

To avoid duplication issues, you want to follow Scott Meyers' advice of make ALL non-leaf classes abstract. Then your base class(s) will have pure accessors, and only the (effective) leaves will implement the data and provide functional accessors.

The only overhead for this is 2 pointers per class (not per instance) for each accessable piece of data in the _vtable. Unless you are working on a VERY small embedded system, this should NEVER be an issue...

scellers
June 26th, 2008, 04:17 PM
Sorry for not explaining that very well, but TheCPUWizard explained exactly what I wanted to know. : ) Thanks a lot!
And yes, the problem was whether I could have class-level data that was polymorphic.