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

Communication between classes

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


P: n/a

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bl**********@nic.grnet.gr...
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


Jul 19 '05 #2

P: n/a
> > 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?


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?
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.