Vectors of Pointers, inheritance
Posted
by user308553
on Stack Overflow
See other posts from Stack Overflow
or by user308553
Published on 2010-04-04T03:12:38Z
Indexed on
2010/04/04
3:23 UTC
Read the original article
Hit count: 294
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
© Stack Overflow or respective owner