Base class manipulation of a derived class' structure

V

Victor Hannak

I have two classes derived from a base class. The two derived classes each
utilize a structure that is slightly different from one another. i.e.

DerivedClass1:

struct NodeStruct {
float NodeValue;
ListStruct *NextNode;
}

DerivedClass2:

struct NodeStruct {
int NodeValue1;
int NodeValue2;
ListStruct *NextNode;
}

I would like to put a function in the base class to delete linked lists
created with these structs. The problem is that these structures are not
declared in the base class, nor can they be because each derived class
requires a slightly different structure. So the question is, how can I
reference a structure that will be declared in the derived class?

I am thinking that this is similar to a virtual function where it is
dynamically determined which derived class function to call. In my case, I
would like to define a generic structure pointer such that when the base
class function is called, the right structure is used according to which
derived class I am operating on.

Thanks, Vic
 
V

Victor Bazarov

Victor Hannak said:
I have two classes derived from a base class. The two derived classes each
utilize a structure that is slightly different from one another. i.e.

DerivedClass1:

struct NodeStruct {
float NodeValue;
ListStruct *NextNode;
}

DerivedClass2:

struct NodeStruct {
int NodeValue1;
int NodeValue2;
ListStruct *NextNode;
}

I would like to put a function in the base class to delete linked lists
created with these structs. The problem is that these structures are not
declared in the base class, nor can they be because each derived class
requires a slightly different structure. So the question is, how can I
reference a structure that will be declared in the derived class?

So the answer is, you cannot. It's simple. You cannot reference
anything that hasn't been declared yet. What you can do is to make
each of the derived classes do their own cleanup. Polymorphically.
I am thinking that this is similar to a virtual function where it is
dynamically determined which derived class function to call. In my case, I
would like to define a generic structure pointer such that when the base
class function is called, the right structure is used according to which
derived class I am operating on.

class BaseClass {
virtual void clearList() = 0;
};

class DerivedClass1 : public BaseClass {
void clearList(); // do what #1 needs
};

class DerivedClass2 : public BaseClass {
void clearList(); // do what #2 needs
};

You _could_ of course, define some kind of LinkStruct class in the
Base, then define a data _member_ a pointer to that type, but then
you would need to make DerivedClass1 and DerivedClass2 create those
objects in the derived class objects dynamically and it's not any
easier or prettier.

Victor
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top