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