By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,822 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

Base class manipulation of a derived class' structure

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Victor Hannak" <vi***********@nospam.kodak.com> wrote...
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
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.