Hi everybody! I´m trying to make this program where i store in a vector different types of derived objects from a single base class. So i declare the vector like this:
vector<BaseClass> vec;
then, assuming that all derived objects are, naturally, base class objects i push a DerivedClass object into the vector:
vec.push_back(new DerivedClass(...));
When i pop one object from the vector, to know what kind of object i´m dealing with, i made the atribute "type" and the respective get method(getType()) in the base class. Then i do the following, wich gives a cast compilation error :
for(int i=0; i<vec.size(); i++){
if(vec.getType() == ...)
DerivedClass *obj = dynamic_cast<DerivedClass*> (vec);
}
In Java I know that this works perfectly even with arrays. For example:
BaseClass array[] = new BasClass[10];
array[0] = new DerivedClass(...);
DerivedClass obj = (DerivedClass)array[0];
The above code works in Java. How do i do the same in c++?
I´d be very gratefull for any help with this. Thanks for your attention.
vector<BaseClass> vec;
then, assuming that all derived objects are, naturally, base class objects i push a DerivedClass object into the vector:
vec.push_back(new DerivedClass(...));
When i pop one object from the vector, to know what kind of object i´m dealing with, i made the atribute "type" and the respective get method(getType()) in the base class. Then i do the following, wich gives a cast compilation error :
for(int i=0; i<vec.size(); i++){
if(vec.getType() == ...)
DerivedClass *obj = dynamic_cast<DerivedClass*> (vec);
}
In Java I know that this works perfectly even with arrays. For example:
BaseClass array[] = new BasClass[10];
array[0] = new DerivedClass(...);
DerivedClass obj = (DerivedClass)array[0];
The above code works in Java. How do i do the same in c++?
I´d be very gratefull for any help with this. Thanks for your attention.