473,385 Members | 1,942 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,385 software developers and data experts.

Base Class pointers to Derived Classes calling functions.

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...
Dec 30 '07 #1
12 2827
On Dec 30, 2:03 am, bgold <bgol...@gmail.comwrote:
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
Dec 30 '07 #2
bgold <bg*****@gmail.comwrote:
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.
Dec 30 '07 #3
On Dec 30, 3:33 pm, sonison.ja...@gmail.com wrote:
On Dec 30, 2:03 am, bgold <bgol...@gmail.comwrote:
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...
Dec 30 '07 #4
On Dec 30, 3:03 pm, bgold <bgol...@gmail.comwrote:
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?
Dec 30 '07 #5
On Dec 30, 12:34 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 30, 3:03 pm, bgold <bgol...@gmail.comwrote:
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.)?
Dec 30 '07 #6
bgold <bg*****@gmail.comwrote:
On Dec 30, 12:34 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 30, 3:03 pm, bgold <bgol...@gmail.comwrote:
[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
Dec 30 '07 #7
bgold wrote:
On Dec 30, 12:34 pm, Rahul <sam_...@yahoo.co.inwrote:
>On Dec 30, 3:03 pm, bgold <bgol...@gmail.comwrote:
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
Dec 30 '07 #8
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.
Dec 31 '07 #9
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.
Dec 31 '07 #10
On Dec 31, 7:05 pm, Ron Natalie <r...@spamcop.netwrote:
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.
I meant the compilation error, one gets with dynamic_cast for the
following cases,

class A
{
};

class B : public A
{
};

int main()
{
B *ptr1;
A *ptr2 = new B();
ptr1 = dynamic_cast<B*>(ptr2); // gives a compilation
error
if(ptr1 == NULL)
printf("fail\n");
else
printf("success\n");
return(0);
}

but typeid doesn't give any such error,

A *ptr = new B();
cout<<typeid(ptr).name()<<endl;
cout<<typeid(*ptr).name()<<endl; // Yes this would give A
without any virtual functions in A, but atleast works...
Dec 31 '07 #11
Rahul wrote:
On Dec 31, 7:05 pm, Ron Natalie <r...@spamcop.netwrote:
>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.

I meant the compilation error, one gets with dynamic_cast for the
following cases,

ptr1 = dynamic_cast<B*>(ptr2); // gives a compilation
error
If your compiler gives a compilation error there it is seriously busted.
Dynamic cast has a whole slew of cases that apply EVEN if the classes
aren't polymorphic.

A warning perhaps, but the program is well formed.
Dec 31 '07 #12
On Dec 31 2007, 5:20 pm, Ron Natalie <r...@spamcop.netwrote:
Rahul wrote:
On Dec 31, 7:05 pm, Ron Natalie <r...@spamcop.netwrote:
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.
I meant the compilation error, one gets with dynamic_cast for the
following cases,
ptr1 = dynamic_cast<B*>(ptr2); // gives a compilation
error
If your compiler gives a compilation error there it is
seriously busted. Dynamic cast has a whole slew of cases that
apply EVEN if the classes aren't polymorphic.
True, but pointer to base to pointer to derived isn't one of
them. The above code should result in a compiler error.
(Remember that ptr2 has type A*, and that A is a base class of
B.)

The reverse, of course, should work.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 1 '08 #13

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

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
2
by: Luca | last post by:
Hi, I have a quite complex question to ask you: I have defined a base class where I would like to have a map holding pointers to member functions defined in derived classes. To be more precise...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
5
by: Michael | last post by:
Hi, Could you tell me whether the following two statement are the same? Derived class is derived from Base class. 1. Base* ptr1; 2. Derived * ptr2; Does it mean ptr1 is the same as ptr2?
5
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
1
by: Rune Allnor | last post by:
Hi all. I am sure this is an oldie, but I can't find a useful suggestion on how to solve it. I have a class hierarchy of classes derived from a base class. I would like to set up a vector of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.