I have struct A and struct B, how could I achieve polymorhism for this case when I need to store the objects in a vector in f and it has parameter of base class A
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
struct A
{
void virtual print()
{
std::cout << "a\n";
}
};
struct B : public A
{
void print() override
{
std::cout << "b\n";
}
};
void f(A& a)
{
// here I don't know what kind of object is that's why I use A
std::vector<std::unique_ptr<A>> vecA;
vecA.emplace_back(std::make_unique<A>(a));
vecA[0]->print();
// I want to print b
}
int main()
{
B b;
f(b);
return 0;
}