473,225 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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 1548
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
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
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
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
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
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
by: Ismail Rajput | last post by:
Is there any option we can use Composite DataKeyField in the DataList and DataGrid?
1
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
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
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
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.