Vectors of Pointers, inheritance
- by user308553
Hi I am a C++ beginner just encountered a problem I don't know how to fix
I have two class, this is the header file:
class A
{
public:
int i;
A(int a);
};
class B: public A
{
public:
string str;
B(int a, string b);
};
then I want to create a vector in main which store either class A or class B
vector<A*> vec;
A objectOne(1);
B objectTwo(2, "hi");
vec.push_back(&objectOne);
vec.push_back(&objectTwo);
cout << vec.at(1)->i; //this is fine
cout << vec.at(1)->str; //ERROR here
I am really confused, I checked sites and stuff but I just don't know how to fix it, please help
thanks in advance