Communication between classes

G

Guest

I have this class
-------------
class Component {
/*class*/ Data *d;
/*class*/ Draw *a;
};
-------------
from "Component" derive classes like "TextBox", "Button", "Label", "ComboBox" etc
from "Draw" derive classes like "TextBoxDraw", "ButtonDraw", "LabelDraw", "ComboBoxDraw" etc
from "Data" derive classes like "TextBoxData", "ButtonData", "LabelData", "ComboBoxData" etc

class "Draw" (or any derived class) gets data from class "Data" (or corresponding derived class)
different derived classes may get different data but corresponding classes communicate with the same data.
Derived classes upcast because of "Data" & "Draw" in member list of "Component"

So, which is the better method for communication between 2 member classes?

My approach until now is a variable length argument list without variable type like this:
------------------------
Data::getMessage(int message, ...);
------------------------
But this approach is very unsafe

do you have any better ideas?

Thanks!
 
H

Howard

I have this class
-------------
class Component {
/*class*/ Data *d;
/*class*/ Draw *a;
};
-------------
from "Component" derive classes like "TextBox", "Button", "Label", "ComboBox" etc
from "Draw" derive classes like "TextBoxDraw", "ButtonDraw", "LabelDraw", "ComboBoxDraw" etc
from "Data" derive classes like "TextBoxData", "ButtonData", "LabelData", "ComboBoxData" etc

class "Draw" (or any derived class) gets data from class "Data" (or corresponding derived class)
different derived classes may get different data but corresponding classes
communicate with the same data.
Derived classes upcast because of "Data" & "Draw" in member list of "Component"

So, which is the better method for communication between 2 member classes?

My approach until now is a variable length argument list without variable type like this:
------------------------
Data::getMessage(int message, ...);
------------------------
But this approach is very unsafe

do you have any better ideas?

Thanks!

You are not being very clear about the structure you have. Can you post the
actual code?

If what you're trying to do is use base-class pointers to access derived
class membersm then that is handled automatically by using virtual functions
and overriding them in the derived classes, provided, when you actually
create the members (in your derived classes), that you create them as
instances of their appropriate derived classes. You declare the member
pointers as their base class pointers, but you create the instances as
derived class instances. Make sense?

-Howard
 
G

Guest

I have this class
communicate with the same data.

You are not being very clear about the structure you have. Can you post the
actual code?

If what you're trying to do is use base-class pointers to access derived
class membersm then that is handled automatically by using virtual functions
and overriding them in the derived classes, provided, when you actually
create the members (in your derived classes), that you create them as
instances of their appropriate derived classes. You declare the member
pointers as their base class pointers, but you create the instances as
derived class instances. Make sense?

Yes. Derived class DerivedData from class Data, can overide virtual void Data::getMessage(int &a). But DerivedData wants give
different data from int& (e.g. float&) DerivedData::getMessage(float &a). Corresponding derived class DerivedDraw from Draw can
handle correctly this data (float&) instead of Draw which can handle correctly data int&.

So, I believe approach like this is the best but unsafe: virtual void Data::getMessage(...)
do you have any better ideas?

If you dont understand again think this:

We have a ComboBox : public Component
TextComboData::getMessage returns an array of strings which is the ComboBox list.
ColorComboData::getMessage returns an array of colors which is the ComboBox list.
TextComboDraw::getMessage gets an array of strings which is the ComboBox list and draws it.
ColorComboDraw::getMessage gets an array of colors which is the ComboBox list and draws it.

Different derived classes communicate with completely different data. So, how can handle this in upcasting?
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top