overloading with class objects in signature

Discussion in 'C++' started by Daniel Luis dos Santos, Apr 13, 2009.

  1. 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 ?
    Daniel Luis dos Santos, Apr 13, 2009
    #1
    1. Advertising

  2. 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-lite/how-to-post.html#faq-5.8
    Vladimir Jovic, Apr 14, 2009
    #2
    1. Advertising

  3. Daniel Luis dos Santos

    red floyd Guest

    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".
    red floyd, Apr 14, 2009
    #3
  4. Daniel Luis dos Santos

    James Kanze Guest

    On Apr 13, 11:10 pm, 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.


    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
    James Kanze, Apr 14, 2009
    #4
  5. Daniel Luis dos Santos

    James Kanze Guest

    On Apr 14, 3:42 pm, red floyd <> 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
    James Kanze, Apr 14, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,147
    Joerg Jooss
    Apr 24, 2004
  2. Replies:
    0
    Views:
    3,156
  3. 7stud
    Replies:
    11
    Views:
    689
    Dennis Lee Bieber
    Mar 20, 2007
  4. Gustavo Narea
    Replies:
    14
    Views:
    865
    Gustavo Narea
    Feb 16, 2009
  5. Michael Rudolf
    Replies:
    13
    Views:
    1,228
    Michael Rudolf
    Feb 28, 2010
Loading...

Share This Page