I'd accept
a pointer to a base class and then call different functions based on its derived type.
[ EDIT
Problem is: accept is a public method of a Manager class, which handles and stores lots of A, B1, B2 (shared_ptr of) instances. Manager will deal with them, based on runtime actual type
EDIT ]
#include <memory>
#include <iostream>
class A {};
class B1 : public A {};
class B2 : public A {};
void accept(std::shared_ptr<B1> sp) { std::cout << "B1 "; }
// How to ensure the overload call to right derived type ?
void accept(std::shared_ptr<A> sp) {
std::cout << "A ";
// if runtime type of sp.get is B1, do something
// elif is B2, something else...
// then if is A, do another thing
}
void accept(std::shared_ptr<B2> sp) { std::cout << "B2 "; }
int main(int argc, char**argv)
{
auto a = std::make_shared<A>();
auto b1 = std::make_shared<B1>();
auto b2 = std::make_shared<B2>();
std::shared_ptr<A> ab2 = std::make_shared<B2>(): // !!!
accept(a);
accept(b1);
accept(b2);
accept(ab2); // !!!
}
Output is A B1 B2 A
. I want B2
for the latter.
Am I misunderstanding inheritance ?