473,493 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
1 2073
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1938
by: Michael Young | last post by:
I've been trying to compile code similar to the example code below, but I keep getting errors indicating that the function 'foo()' is not accessible. At first, I thought this was a bug in the...
4
2835
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
5
3373
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member...
6
8131
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7367
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.