Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: c++ downcasting

Status
Not open for further replies.

susy123

Programmer
Jul 31, 2006
2
PT
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.
 
Code:
vector<BaseClass> vec;
should be
Code:
vector<BaseClass*> vec;
In Java, what you store in the array is basically a pointer, so you have to store pointers in the vector.

Everything else should then work, even though it might be poor design. Also, don't forget to delete the objects later, and make sure BaseClass has a virtual destructor.
 
One other thing, if your "type" system is always accurate, then use static_cast instead of dynamic_cast. Using dynamic_cast allows you to test whether you are casting to the appropriate type, but if you already know that you are based on getType, then you don't need the extra cost of the runtime check.
 
Thank u so much Uolj, at last i made the program work. I confess that i´m a novice in c++ and i really dont see other way than this to make a program where different derived class objects are stored in a vector or array. Why is this a poor programming practice? Does anyone knows other way?
 
Sorry, I should have been a bit more clear. It's not the vector of base class pointers that is potentially bad design. That is a very common way of doing things.

What I was referring to was the use of the cast. I believe it is something that is applicable in Java as well as in C++. When you have a class hierarchy, each derived class should work wherever you have a base class pointer, since each derived class instance is a base class instance. In your example, only a certain type of derived class will be able to run the code inside the if. Such specialized code often indicates that the derived class shouldn't actually be derived from that base class, or that the action should be implemented with a virtual function in the base class that can be called on any base class instance, even those that are in fact of a different derived class type.

There are legitimate uses of dynamic_cast (or getType() and static_cast), so it's hard to tell whether you should consider re-thinking your design or not. Once you start putting all that if (getType() == WHATEVER) code in, it gets to be a maintenance headache, so if you have the time now might be good to do that reconsidering.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top