overloading with class objects in signature

  • Thread starter Daniel Luis dos Santos
  • Start date
D

Daniel Luis dos Santos

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 ?
 
V

Vladimir Jovic

Daniel said:
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-lite/how-to-post.html#faq-5.8
 
R

red floyd

Daniel said:
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".
 
J

James Kanze

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 :

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.
 
J

James Kanze

Daniel Luis dos Santos wrote:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top