Base Class pointers to Derived Classes calling functions.

Discussion in 'C++' started by bgold, Dec 30, 2007.

  1. bgold

    bgold Guest

    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...
     
    bgold, Dec 30, 2007
    #1
    1. Advertising

  2. bgold

    Guest

    On Dec 30, 2:03 am, bgold <> 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
     
    , Dec 30, 2007
    #2
    1. Advertising

  3. bgold

    Daniel T. Guest

    bgold <> 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.
     
    Daniel T., Dec 30, 2007
    #3
  4. bgold

    Rahul Guest

    On Dec 30, 3:33 pm, wrote:
    > On Dec 30, 2:03 am, bgold <> 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...
     
    Rahul, Dec 30, 2007
    #4
  5. bgold

    Rahul Guest

    On Dec 30, 3:03 pm, bgold <> 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?
     
    Rahul, Dec 30, 2007
    #5
  6. bgold

    bgold Guest

    On Dec 30, 12:34 pm, Rahul <> wrote:
    > On Dec 30, 3:03 pm, bgold <> 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.)?
     
    bgold, Dec 30, 2007
    #6
  7. bgold

    Daniel T. Guest

    bgold <> wrote:
    > On Dec 30, 12:34 pm, Rahul <> wrote:
    > > On Dec 30, 3:03 pm, bgold <> 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
     
    Daniel T., Dec 30, 2007
    #7
  8. bgold

    Guest

    bgold wrote:

    > On Dec 30, 12:34 pm, Rahul <> wrote:
    >> On Dec 30, 3:03 pm, bgold <> 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
     
    , Dec 30, 2007
    #8
  9. bgold

    bgold Guest

    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.
     
    bgold, Dec 31, 2007
    #9
  10. bgold

    Ron Natalie Guest

    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.
     
    Ron Natalie, Dec 31, 2007
    #10
  11. bgold

    Rahul Guest

    On Dec 31, 7:05 pm, Ron Natalie <> wrote:
    > 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...
     
    Rahul, Dec 31, 2007
    #11
  12. bgold

    Ron Natalie Guest

    Rahul wrote:
    > On Dec 31, 7:05 pm, Ron Natalie <> wrote:
    >> 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.
     
    Ron Natalie, Dec 31, 2007
    #12
  13. bgold

    James Kanze Guest

    On Dec 31 2007, 5:20 pm, Ron Natalie <> wrote:
    > Rahul wrote:
    > > On Dec 31, 7:05 pm, Ron Natalie <> wrote:
    > >> 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:
    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, Jan 1, 2008
    #13
    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. Luca
    Replies:
    2
    Views:
    499
    Rob Williscroft
    Dec 19, 2003
  2. Dave
    Replies:
    2
    Views:
    4,175
    Donovan Rebbechi
    May 16, 2005
  3. Replies:
    1
    Views:
    405
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    395
    Victor Bazarov
    May 23, 2007
  5. Replies:
    9
    Views:
    333
    James Kanze
    Jul 25, 2008
Loading...

Share This Page