I have two classes A
and B
which derive from the same base class Base
.
I am defining in main a vector Base *
and I use it for storing elements both of A
and B
.
Using a reinterpret_cast I am able to call methods of their real class.
Is this code safe? Is there a better/standard way of doing that?
The two classes in the real code share most of the methods from the base class but the A
possesses some methods not shared with Base
or B
and the same for B
, so virtual is not an option.
#include <iostream>
#include <vector>
class Base {
};
class A : public Base {
public:
void print_from_A() {
std::cout << "hello from A\n";
}
};
class B : public Base {
public:
int number;
void read_from_B() {
std::cin >> number;
}
};
int main(int argc, char * argv[]) {
std::vector<Base*> v;
v.push_back(new A());
v.push_back(new B());
(reinterpret_cast<A *>(v[0]))->print_from_A();
(reinterpret_cast<B *>(v[1]))->read_from_B();
for(auto && e: v) {
delete e;
}
return 0;
}
EDIT:
It seems that using reinterpret_cast
is undefined behaviour: Casting to one class and calling function from sibling class?
It also seems that the correct way is using a static_cast
: Can I get polymorphic behavior without using virtual functions?
Is this the answer?