0

What I'm trying to do is something like this:

void SomeFunction(Base *){//do something}
void SomeFunction(Derived *){//do something else}

Is this possible in C++? My attempts so far just call the base version of the function. Here is an example for some clarity.

Example:

#include <iostream>
#include <vector>

class Base
{
    public:
        Base () {std::cout << "Base Constructor for " << this << std::endl;}
        void virtual PrintSomething ()
        {
            std::cout << "I am Base!" << std::endl;
        };
};

class Derived : public Base
{
    public:
        Derived () : Base () {std::cout << "Derived Construtor for " << this << std::endl;}
        void virtual PrintSomething ()
        {
            std::cout << "I am Derived!" << std::endl;
        };
};

void DoAmazingStuff ( Base * ) {std::cout << "Amazing!" << std::endl;}
void DoAmazingStuff ( Derived * ) {std::cout << "DERP!" << std::endl;}

int main ()
{
    std::vector<Base *> some_vector;

    Base *x = new Base ();
    Derived *y = new Derived ();

    some_vector.push_back ( x );
    some_vector.push_back ( y );

// This is the part that isn't functioning as expected.
/******************************************/
    DoAmazingStuff ( some_vector[0] );
    DoAmazingStuff ( some_vector[1] );
/******************************************/

    std::cin.get ();
    std::cin.get ();
    delete some_vector[0];
    delete some_vector[1];
}

The following line never gets called:

void DoAmazingStuff ( Derived * ) {std::cout << "DERP!" << std::endl;}
4
  • 3
    Can you show us an example on how you would use these functions? Can you perhaps make an SSCCE? Commented May 31, 2013 at 12:04
  • what is your objective? Commented May 31, 2013 at 12:08
  • Put SomeFunction on the class as a virtual function. Commented May 31, 2013 at 12:10
  • added, sorry it took a while! @PeterWood this isn't what I am trying to achieve. Commented May 31, 2013 at 12:49

1 Answer 1

7

Certainly it's possible; but the Derived version will only be called if the static type of the pointer is Derived*. For example:

Base * b = new Base;        // static and dynamic types are Base
Derived * d = new Derived;  // static and dynamic types are Derived
Base * bd = new Derived;    // static type Base, dynamic type Derived

SomeFunction(b);   // Base overload
SomeFunction(d);   // Derived overload
SomeFunction(bd);  // Base overload (from static type)

Dynamic dispatch (selecting a function based on its dynamic type) will only happen when calling virtual member functions, not overloaded non-member functions:

struct Base {
    virtual void SomeFunction() {/*do something*/}
};
struct Derived : Base {
    virtual void SomeFunction() {/*do something else*/}
};

b->SomeFunction();  // Base version
d->SomeFunction();  // Derived override
bd->SomeFunction(); // Derived override (from dynamic type)

and you could achieve something like dynamic dispatch on a non-member function with the help of a member function:

void SomeFunction(Base * b) {b->SomeFunction();}

As noted in the comments, this technique can be extended to implement multiple dispatch, selecting a function based on the dynamic types of multiple function arguments. But that's rather beyond the scope of the question.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. It might also be worth mentioning that if dynamic dispatch is desired in this case, the terms to search for are "double dispatch" or "visitor pattern."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.