![]() |
Base class manipulation of a derived class' structure
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 |
Re: Base class manipulation of a derived class' structure
"Victor Hannak" <victor.hannak@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 |
| All times are GMT. The time now is 02:00 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.