I'm trying to have an boost::container::vector containing instances of a few class that all inherit from the same superclass, and then have some functions only apply to the instances of a specific class, but I can't seem to get it to work.
This is the first time I try this, so maybe I just made a rookie mistake, can one of you tell me where I went wrong, and why ?
Here's an example code :
using boost::container::vector;
class card{
public:
card(int aa,int bb):a(aa),b(bb){}
virtual ~card(){}
int a,b;
};
bool operator==(card l, card r){ return l.a==r.a; }
bool operator<(card l, card r){
if(l.a<r.a){ return true;}
else{ return l.b<r.b; }
}
std::ostream& operator<<(std::ostream& os, card c){
os << c.a << ":" << c.b;
return os;
}
class subcard:public card{
public:
subcard(int a, int b):card(a,b){}
virtual ~subcard(){}
int c=0;
};
int main() {
cout << "Hello, World!" << endl;
vector<card> v1;
v1.push_back(card(2,2));
v1.push_back(subcard(1,1));
for (int i = 0; i < v1.size(); ++i) {
cout<<v1[i]<<endl;
}
for (int i = 0; i < v1.size(); ++i) {
if(subcard* sc = dynamic_cast<subcard*>(&(v1[i]))){
cout<< "found : " << sc <<endl;
}
}
return 0;
}
Thank you very much :)