Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Base class manipulation of a derived class' structure

Reply
Thread Tools

Base class manipulation of a derived class' structure

 
 
Victor Hannak
Guest
Posts: n/a
 
      08-25-2003
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-25-2003
"Victor Hannak" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from base to derived class in base constructor pastbin@gmail.com C++ 2 02-07-2008 02:41 PM
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments