Ian Thompson Ian Thompson - 1 month ago 10
C++ Question

How to call derived class method from base class pointer?

I have a class structure similar to the following

class A
{
public:
A(void);
~A(void);

void DoSomething(int i)
{
std::cout << "Hello A" << i << std::endl;
}
};

class B : public A
{
public:
B(void);
~B(void);

void DoSomething(int i)
{
std::cout << "Hello B" << i << std::endl;
}
};

class Ad : public A
{
public:
Ad(void);
~Ad(void);
};

class Bd : public B
{
public:
Bd(void);
~Bd(void);
};


I want to store instances of the derived classes in a container (standard map) as a collection of A*, then iterate through the container and call methods for each instance.

#include "A.h"
#include "B.h"
#include "Ad.h"
#include "Bd.h"
#include <map>
int main(int argc, char** argv)
{
std::map<int,A*> objectmap;
objectmap[1] = new Ad();
objectmap[2] = new Bd();

for (std::map<int,A*>::iterator itrobject = objectmap.begin();
itrobject!=objectmap.end(); itrobject++)
{
itrobject->second->DoSomething(1);
}
return 0;
}


The above code produces the following output.



Hello A1
Hello A1


Where I was expecting



Hello A1
Hello B1


because I was expecting DoSomething in B to hide DoSomething in A, and because I am storing A pointers, I would expect no object slicing (and looking at the object pointer in the debugger shows that the object has not been sliced).

I have tried down casting and dynamic casting the pointer to B, but it slices away the data members of Bd.

Is there any way to call B::DoSomething without casting the pointer to Bd? And if not, if I have many derived classes of B (e.g. Bda, Bdb, Bdc etc), is there some way to use RTTI to know which derived class to cast it to?

Answer

You need to make DoSomething() a virtual function in both classes to get the polymorphic behavior you're after:

virtual void DoSomething(int i) { ...

EDIT: to show you don't need to implement virtual functions in every sub class:

#include <iostream>

class A {
    public:
        virtual void print_me(void) {
            std::cout << "I'm A" << std::endl;
        }

        virtual ~A() {}
};

class B : public A {
    public:
        virtual void print_me(void) {
            std::cout << "I'm B" << std::endl;
        }
};

class C : public A {
};

int main() {

    A a;
    B b;
    C c;

    A* p = &a;
    p->print_me();

    p = &b;
    p->print_me();

    p = &c;
    p->print_me();

    return 0;
}

outputs:

I'm A
I'm B
I'm A