473,511 Members | 15,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

composite

bob
Hi,

we are using the composite pattern which simply has a common abstract
base class. Deriving from this class there is a Comp class, and a
CompItem class, say. i.e.

abstractBase
| \
| \
v \
Comp CompItem
|
|
V
CompItemSubClass
We have also the CompItemSubClass.

Now we can either have pointers to CompItems or CompItemSubClasses. I
want to be able to call the correct function (without dynamic casting
if possible).

e.g.

virtual void toto (CompItem* compItem);
virtual void toto (CompItemSubClass* compItemSubClass);
we have something like this;

CompItem* tmp=someFunc(); // creates either a CompItems
orCompItemSubClass

then I do;

toto(tmp);

which always invokes;

virtual void toto (CompItem* compItem);
Is there any nifty way to have

virtual void toto (CompItemSubClass* compItemSubClass);

called without casting (assuming the tmp contains a pointer to a
CompItemSubClass) ?

Thanks much. Hope that makes sense. I'm rushing here :)

grahamO

Aug 21 '07 #1
3 1555
On Aug 21, 3:54 pm, "b...@blah.com" <GrahamJWa...@gmail.comwrote:
Hi,

we are using the composite pattern which simply has a common abstract
base class. Deriving from this class there is a Comp class, and a
CompItem class, say. i.e.

abstractBase
| \
| \
v \
Comp CompItem
|
|
V
CompItemSubClass

We have also the CompItemSubClass.

Now we can either have pointers to CompItems or CompItemSubClasses. I
want to be able to call the correct function (without dynamic casting
if possible).

e.g.

virtual void toto (CompItem* compItem);
virtual void toto (CompItemSubClass* compItemSubClass);

we have something like this;

CompItem* tmp=someFunc(); // creates either a CompItems
orCompItemSubClass

then I do;

toto(tmp);

which always invokes;

virtual void toto (CompItem* compItem);

Is there any nifty way to have

virtual void toto (CompItemSubClass* compItemSubClass);

called without casting (assuming the tmp contains a pointer to a
CompItemSubClass) ?
One way is to define member functions inside class compItem and
compItemSubClass to perform the tasks that toto does, and then invoke
these functions.

struct compItem
{
virtual void dototo() { /* toto() for compItem */
};

struct compItemSubClass : public compItem
{
virtual void dototo() { /* toto for compItemSubClass */
};

The version of toto that takes CompItem can be defined as:

virtual void toto (CompItem* compItem)
{
compItem->dototo(); // will invoke appropriate dototo()
}
-N

Aug 21 '07 #2
bo*@blah.com wrote:
we are using the composite pattern which simply has a common abstract
base class. Deriving from this class there is a Comp class, and a
CompItem class, say. i.e.

abstractBase
> \
\
v \
Comp CompItem
|
|
V
CompItemSubClass
We have also the CompItemSubClass.

Now we can either have pointers to CompItems or CompItemSubClasses. I
want to be able to call the correct function (without dynamic casting
if possible).

e.g.

virtual void toto (CompItem* compItem);
virtual void toto (CompItemSubClass* compItemSubClass);
we have something like this;

CompItem* tmp=someFunc(); // creates either a CompItems
orCompItemSubClass

then I do;

toto(tmp);

which always invokes;

virtual void toto (CompItem* compItem);
Is there any nifty way to have

virtual void toto (CompItemSubClass* compItemSubClass);

called without casting (assuming the tmp contains a pointer to a
CompItemSubClass) ?

Thanks much. Hope that makes sense. I'm rushing here :)
There is no direct way. If you know that the object a pointer to
which you obtained from 'someFunc' is one of the two (and you are
sure of it), using a static cast would be OK. 'dynamic_cast' is
only needed if you're not sure and it also requires for the class
to be polymorphic (I don't think you mentioned whether it was).

A bit better way is to give the choice to the object itself. That
should essentially be an exercise in double dispatch:

class CompItem : public abstractBase {
...
virtual void callTotoFor(someOtherClass* ptr) {
ptr->toto(this);
}
};

class CompItemSubClass : public CompItem {
...
virtual void callTotoFor(someOtherClass* ptr) {
ptr->toto(this);
}
};

... // inside 'someOtherClass's member function or elsewhere
CompItem *pItem = someFunc();
// blah->toto(pItem); -- this doesn't work well, do:
pItem->callToto(blah);

HTH. Ask more questions as you get them.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 21 '07 #3
bob
On Aug 21, 1:39 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
b...@blah.com wrote:
we are using thecompositepattern which simply has a common abstract
base class. Deriving from this class there is a Comp class, and a
CompItem class, say. i.e.
abstractBase
\
\
v \
Comp CompItem
|
|
V
CompItemSubClass
We have also the CompItemSubClass.
Now we can either have pointers to CompItems or CompItemSubClasses. I
want to be able to call the correct function (without dynamic casting
if possible).
e.g.
virtual void toto (CompItem* compItem);
virtual void toto (CompItemSubClass* compItemSubClass);
we have something like this;
CompItem* tmp=someFunc(); // creates either a CompItems
orCompItemSubClass
then I do;
toto(tmp);
which always invokes;
virtual void toto (CompItem* compItem);
Is there any nifty way to have
virtual void toto (CompItemSubClass* compItemSubClass);
called without casting (assuming the tmp contains a pointer to a
CompItemSubClass) ?
Thanks much. Hope that makes sense. I'm rushing here :)

There is no direct way. If you know that the object a pointer to
which you obtained from 'someFunc' is one of the two (and you are
sure of it), using a static cast would be OK. 'dynamic_cast' is
only needed if you're not sure and it also requires for the class
to be polymorphic (I don't think you mentioned whether it was).

A bit better way is to give the choice to the object itself. That
should essentially be an exercise in double dispatch:

class CompItem : public abstractBase {
...
virtual void callTotoFor(someOtherClass* ptr) {
ptr->toto(this);
}
};

class CompItemSubClass : public CompItem {
...
virtual void callTotoFor(someOtherClass* ptr) {
ptr->toto(this);
}
};

... // inside 'someOtherClass's member function or elsewhere
CompItem *pItem = someFunc();
// blah->toto(pItem); -- this doesn't work well, do:
pItem->callToto(blah);

HTH. Ask more questions as you get them.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
thanks very much for that reply. Spot on. Cheers, G

Aug 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1210
by: Ken | last post by:
Our tool control applicatioin is currently comprised of a .NET part written in C#, components written in C++ and a Lab View component. They are at different version levels. Not all composite...
0
1435
by: Michael Andersson | last post by:
Given a set of classes class A { enum [ ID = 0x0001} }; class B { enum [ ID = 0x0002} }; class B { enum [ ID = 0x0004} }; I wish to generate a composite class, perhaps using something like...
0
2391
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
18
12623
by: Thomas A. Anderson | last post by:
I am a bit confused in creating a composite primary key. I have three table with two of the tables containing primary keys. I have two of the tables (each with a primary key) having one to many...
0
1576
by: Satya Bojanapally | last post by:
Hi, I am unable to add a pager for this composite control. I had created a composite control in C#. The control is having 5 labels, one radio button and one DropDownList control. The composite...
4
4042
by: Ismail Rajput | last post by:
Is there any option we can use Composite DataKeyField in the DataList and DataGrid?
1
3136
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
4
4079
by: Mark Olbert | last post by:
This involves a family of related, databound ASPNET2 composite controls. I've managed to arrange things so that the composite controls restore themselves from ViewState on postback after they're...
14
5005
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
3
1937
by: Eric | last post by:
I have created a fairly basic composite control consisting of a Label and a TextBox. In the overridden Render function, I'm creating a table with two rows and each row contains a cell (td). The...
0
7245
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
7427
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
7085
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
5671
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,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
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
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.