Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading with class objects in signature

Reply
Thread Tools

overloading with class objects in signature

 
 
Daniel Luis dos Santos
Guest
Posts: n/a
 
      04-13-2009
Hello,

I have two classes A and B. B is a subclass of A.
I then have another class C. This one has the following methods :

int doSomething(A *obj);
int doSomething(B *obj);

C also has as a member an instance of A,

private:
A *obj;

In one of C's methods I have in the previous attribute an instance of
B, with which I will call :

doSomething(obj);

Stepping through this, I noticed that the "int doSomething(A *obj)" is
called instead of the other, although the instance at runtime is of
class B. I guess that's because obj was declared as being of type A. I
was expecting that the other method would be called.

Is there some other way of doing this ?

 
Reply With Quote
 
 
 
 
Vladimir Jovic
Guest
Posts: n/a
 
      04-14-2009
Daniel Luis dos Santos wrote:
> Hello,
>
> I have two classes A and B. B is a subclass of A.
> I then have another class C. This one has the following methods :
>
> int doSomething(A *obj);
> int doSomething(B *obj);
>
> C also has as a member an instance of A,
>
> private:
> A *obj;
>
> In one of C's methods I have in the previous attribute an instance of B,
> with which I will call :
>
> doSomething(obj);
>
> Stepping through this, I noticed that the "int doSomething(A *obj)" is
> called instead of the other, although the instance at runtime is of
> class B. I guess that's because obj was declared as being of type A. I
> was expecting that the other method would be called.
>
> Is there some other way of doing this ?
>


Off course there is.

This might help you solve the problem:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      04-14-2009
Daniel Luis dos Santos wrote:
> Hello,
>
> I have two classes A and B. B is a subclass of A.
> I then have another class C. This one has the following methods :
>
> int doSomething(A *obj);
> int doSomething(B *obj);
>
> C also has as a member an instance of A,
>
> private:
> A *obj;
>
> In one of C's methods I have in the previous attribute an instance of B,
> with which I will call :
>
> doSomething(obj);
>
> Stepping through this, I noticed that the "int doSomething(A *obj)" is
> called instead of the other, although the instance at runtime is of
> class B. I guess that's because obj was declared as being of type A. I
> was expecting that the other method would be called.
>
> Is there some other way of doing this ?
>

Google for "double dispatch".
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-14-2009
On Apr 13, 11:10 pm, Daniel Luis dos Santos <daniel.d...@gmail.com>
wrote:

> I have two classes A and B. B is a subclass of A.
> I then have another class C. This one has the following
> methods :


> int doSomething(A *obj);
> int doSomething(B *obj);


> C also has as a member an instance of A,


> private:
> A *obj;


> In one of C's methods I have in the previous attribute an
> instance of B, with which I will call :


> doSomething(obj);


> Stepping through this, I noticed that the "int doSomething(A
> *obj)" is called instead of the other, although the instance
> at runtime is of class B. I guess that's because obj was
> declared as being of type A. I was expecting that the other
> method would be called.


Why? doSomething isn't a virtual function of A.

> Is there some other way of doing this ?


Make doSomething a virtual function of A.

--
James Kanze (GABI Software) email:
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-14-2009
On Apr 14, 3:42 pm, red floyd <no.spam.h...@example.com> wrote:
> Daniel Luis dos Santos wrote:


> > I have two classes A and B. B is a subclass of A. I then
> > have another class C. This one has the following methods :


> > int doSomething(A *obj);
> > int doSomething(B *obj);


> > C also has as a member an instance of A,


> > private:
> > A *obj;


> > In one of C's methods I have in the previous attribute an
> > instance of B, with which I will call :


> > doSomething(obj);


> > Stepping through this, I noticed that the "int doSomething(A
> > *obj)" is called instead of the other, although the instance
> > at runtime is of class B. I guess that's because obj was
> > declared as being of type A. I was expecting that the other
> > method would be called.


> > Is there some other way of doing this ?


> Google for "double dispatch".


Except that there's no double dispatch involved. He wants
dynamic dispatch on a non-member function, which C++ doesn't
support. (It's possible to emulate it, of course, if you really
have to, but the correct solution is almost always to use
virtual member functions.)

--
James Kanze (GABI Software) email:
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
 
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
Signature-based Function Overloading in Python Michael Rudolf Python 13 02-28-2010 06:07 AM
Turning a signature-changing decorator into a signature-preservingone Gustavo Narea Python 14 02-16-2009 04:58 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
UTF-8 with signature & UTF-8 without signature JJBW ASP .Net 1 04-24-2004 10:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57