Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Base Class pointers to Derived Classes calling functions.

Reply
Thread Tools

Base Class pointers to Derived Classes calling functions.

 
 
bgold
Guest
Posts: n/a
 
      12-30-2007
Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair of
pointers, where each is a pointer to the base class (each is a SPRITE
*). I know that each of these pointers actually points to one of the
derived classes, even though the type of the pointer is SPRITE *, but
I don't know which derived class it points to. I would like to write a
set of functions that do something different depending on what
combination of derived classes the two pointers point to (i.e. I want
one function for a PERSON * and BULLET *, a second function for a
PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

How can I write these functions, and how do I call them, so that the
correct function is called given the two pointers?

I hope this is clear...
 
Reply With Quote
 
 
 
 
sonison.james@gmail.com
Guest
Posts: n/a
 
      12-30-2007
On Dec 30, 2:03 am, bgold <(E-Mail Removed)> wrote:
> Hey. I have a base class (SPRITE), and using this base class I have
> derived a large number of derived classes (PERSON, BULLET, MISSILE,
> etc.). Now, at a certain point in my program, I have a pair of
> pointers, where each is a pointer to the base class (each is a SPRITE
> *). I know that each of these pointers actually points to one of the
> derived classes, even though the type of the pointer is SPRITE *, but
> I don't know which derived class it points to. I would like to write a
> set of functions that do something different depending on what
> combination of derived classes the two pointers point to (i.e. I want
> one function for a PERSON * and BULLET *, a second function for a
> PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).
>
> How can I write these functions, and how do I call them, so that the
> correct function is called given the two pointers?
>
> I hope this is clear...


Not sure if what you want is an overloaded function to automatically
figure out the correct overload based on the pointer; if that's the
case then I don't think it would be possible with pointers.

You could however, use dynamic_cast(or even typeid) to figure out what
the pointer is pointing to and then make the appropriate set of calls
in the code. Though this design would not scale well as you add more
classes.

Thanks and regards
Sonison James
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      12-30-2007
bgold <(E-Mail Removed)> wrote:

> Hey. I have a base class (SPRITE), and using this base class I have
> derived a large number of derived classes (PERSON, BULLET, MISSILE,
> etc.). Now, at a certain point in my program, I have a pair of
> pointers, where each is a pointer to the base class (each is a SPRITE
> *). I know that each of these pointers actually points to one of the
> derived classes, even though the type of the pointer is SPRITE *, but
> I don't know which derived class it points to. I would like to write a
> set of functions that do something different depending on what
> combination of derived classes the two pointers point to (i.e. I want
> one function for a PERSON * and BULLET *, a second function for a
> PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).
>
> How can I write these functions, and how do I call them, so that the
> correct function is called given the two pointers?
>
> I hope this is clear...


Use the Visitor pattern. Better yet, redesign your program so you don't
have such a situation.

Wikipedia has a nice article on the Visitor Pattern, including sample
code (in Java.) Check it out.
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      12-30-2007
On Dec 30, 3:33 pm, (E-Mail Removed) wrote:
> On Dec 30, 2:03 am, bgold <(E-Mail Removed)> wrote:
>
>
>
> > Hey. I have a base class (SPRITE), and using this base class I have
> > derived a large number of derived classes (PERSON, BULLET, MISSILE,
> > etc.). Now, at a certain point in my program, I have a pair of
> > pointers, where each is a pointer to the base class (each is a SPRITE
> > *). I know that each of these pointers actually points to one of the
> > derived classes, even though the type of the pointer is SPRITE *, but
> > I don't know which derived class it points to. I would like to write a
> > set of functions that do something different depending on what
> > combination of derived classes the two pointers point to (i.e. I want
> > one function for a PERSON * and BULLET *, a second function for a
> > PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

>
> > How can I write these functions, and how do I call them, so that the
> > correct function is called given the two pointers?

>
> > I hope this is clear...

>
> Not sure if what you want is an overloaded function to automatically
> figure out the correct overload based on the pointer; if that's the
> case then I don't think it would be possible with pointers.
>
> You could however, use dynamic_cast(or even typeid) to figure out what
> the pointer is pointing to and then make the appropriate set of calls
> in the code. Though this design would not scale well as you add more
> classes.
>
> Thanks and regards
> Sonison James


dynamic_cast would work only with classes having virtual functions,
typeid operator would
be more appropriate...
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      12-30-2007
On Dec 30, 3:03 pm, bgold <(E-Mail Removed)> wrote:
> Hey. I have a base class (SPRITE), and using this base class I have
> derived a large number of derived classes (PERSON, BULLET, MISSILE,
> etc.). Now, at a certain point in my program, I have a pair of
> pointers, where each is a pointer to the base class (each is a SPRITE
> *). I know that each of these pointers actually points to one of the
> derived classes, even though the type of the pointer is SPRITE *, but
> I don't know which derived class it points to. I would like to write a
> set of functions that do something different depending on what
> combination of derived classes the two pointers point to (i.e. I want
> one function for a PERSON * and BULLET *, a second function for a
> PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).
>
> How can I write these functions, and how do I call them, so that the
> correct function is called given the two pointers?
>
> I hope this is clear...


Is this a global function or are you planning to have it as a member
function, is so in which class?
 
Reply With Quote
 
bgold
Guest
Posts: n/a
 
      12-30-2007
On Dec 30, 12:34 pm, Rahul <(E-Mail Removed)> wrote:
> On Dec 30, 3:03 pm, bgold <(E-Mail Removed)> wrote:
>
>
>
> > Hey. I have a base class (SPRITE), and using this base class I have
> > derived a large number of derived classes (PERSON, BULLET, MISSILE,
> > etc.). Now, at a certain point in my program, I have a pair of
> > pointers, where each is a pointer to the base class (each is a SPRITE
> > *). I know that each of these pointers actually points to one of the
> > derived classes, even though the type of the pointer is SPRITE *, but
> > I don't know which derived class it points to. I would like to write a
> > set of functions that do something different depending on what
> > combination of derived classes the two pointers point to (i.e. I want
> > one function for a PERSON * and BULLET *, a second function for a
> > PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

>
> > How can I write these functions, and how do I call them, so that the
> > correct function is called given the two pointers?

>
> > I hope this is clear...

>
> Is this a global function or are you planning to have it as a member
> function, is so in which class?


This is a set of global functions, each of which will take a different
pair of pointers.

I'd rather not use typeid. Isn't there some way of doing overloaded
functions so that I can just have one call that automatically calls
the correct function (for example: OverloadedFunction(PERSON *p,
BULLET *bt); and OverloadedFunction(PERSON *p, MISSILE *ms); etc.)?
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      12-30-2007
bgold <(E-Mail Removed)> wrote:
> On Dec 30, 12:34 pm, Rahul <(E-Mail Removed)> wrote:
> > On Dec 30, 3:03 pm, bgold <(E-Mail Removed)> wrote:
> >
> > > [paraphrase] I need to implement double dispatch but I don't
> > > know how.

> >
> > Is this a global function or are you planning to have it as a
> > member function, is so in which class?

>
> This is a set of global functions, each of which will take a
> different pair of pointers.
>
> I'd rather not use typeid. Isn't there some way of doing overloaded
> functions so that I can just have one call that automatically calls
> the correct function (for example: OverloadedFunction(PERSON *p,
> BULLET *bt); and OverloadedFunction(PERSON *p, MISSILE *ms); etc.)?


No. The Visitor pattern or some variant of it (Acyclic Visitor comes to
mind) is your only option.

http://www.objectmentor.com/resources/articles/acv.pdf
 
Reply With Quote
 
jkherciueh@gmx.net
Guest
Posts: n/a
 
      12-30-2007
bgold wrote:

> On Dec 30, 12:34 pm, Rahul <(E-Mail Removed)> wrote:
>> On Dec 30, 3:03 pm, bgold <(E-Mail Removed)> wrote:
>>
>>
>>
>> > Hey. I have a base class (SPRITE), and using this base class I have
>> > derived a large number of derived classes (PERSON, BULLET, MISSILE,
>> > etc.). Now, at a certain point in my program, I have a pair of
>> > pointers, where each is a pointer to the base class (each is a SPRITE
>> > *). I know that each of these pointers actually points to one of the
>> > derived classes, even though the type of the pointer is SPRITE *, but
>> > I don't know which derived class it points to. I would like to write a
>> > set of functions that do something different depending on what
>> > combination of derived classes the two pointers point to (i.e. I want
>> > one function for a PERSON * and BULLET *, a second function for a
>> > PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

>>
>> > How can I write these functions, and how do I call them, so that the
>> > correct function is called given the two pointers?

>>
>> > I hope this is clear...

>>
>> Is this a global function or are you planning to have it as a member
>> function, is so in which class?

>
> This is a set of global functions, each of which will take a different
> pair of pointers.
>
> I'd rather not use typeid. Isn't there some way of doing overloaded
> functions so that I can just have one call that automatically calls
> the correct function (for example: OverloadedFunction(PERSON *p,
> BULLET *bt); and OverloadedFunction(PERSON *p, MISSILE *ms); etc.)?


Not really. The problem is known as "double dispatch" and there are several
ways to deal with it, but all of them require a certain amount of
scaffolding and support from the classes involved. If C++ had templated
virtual member functions, one could (using some trickery) encode tables to
resolve the calls within the vtables; but since that support is missing
from the language, the usual solutions either involve handcoding the double
dispatch through member functions or setting up a static table manually. I
am sure that google will provide plenty of information about double
dispatch so that you can make an informed decision on how you want to go
about it.


Best

Kai-Uwe Bux
 
Reply With Quote
 
bgold
Guest
Posts: n/a
 
      12-31-2007
Looks like I'm going to have to figure out how to implement this
Visitor pattern, if I can't entirely restructure my program. Thanks
everyone.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-31-2007
Rahul wrote:

>
> dynamic_cast would work only with classes having virtual functions,
> typeid operator would
> be more appropriate...


typeid has the same restrictions. If the class isn't polymorphic
(virtual) functions, the runtime typing info ain't there.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
calling base class destructor implicitly from derived classes Dave C++ 2 05-16-2005 03:04 AM
from base class calling methods defined in derived classes through pointer to member functions Luca C++ 2 12-19-2003 12:31 PM



Advertisments