   c++ derived classes
  hello,i'm trying to convert something from c# to c++ and its apparently been way too long since ive used c++.here's an example of the bit i'm stuck on, this works ok in c#.class Vehicle {}class Car : Vehicle {}class Truck : Vehicle {}Car car = new Car();Truck truck = new Truck();Vehicle vehicle = (Vehicle)car;if (vehicle is Car){    // do car stuff}else if (vehicle is Truck){    // do truck stuff}my question is, what is the c++ equivalent of "if (vehicle is Car)" ?thanks, Nat.   
  C++ provides the typeid operator, which returns a type_info object with information about the data type. There's also dynamic_cast, which allows you to "attempt" a cast from a class A to a class B. If class B is derived from A the cast will return a valid pointer. If not, it will return NULL:Vehicle* someVehicle = ...; // we don't know the actual typeCar* car = dynamic_cast<Car*>(someVehicle);if(car != NULL){  // it's a car}Note that using RTTI (Run-Time Type Information) in this manner is frowned upon because it breaks the Open/Closed Principle leading to code that is difficult to maintain and extend. You may want to consider moving the code specific to each type of vehicle into a virtual function exposed by the base class.EDIT: On a side note, dynamic_cast will obviously not return NULL when used with references. In that case you'll want to watch out for an std::bad_cast exception.   ;  You can use abstract functions for this kind of stuff:class Vehicle {public:virtual void DoSth()=0; //pure abstract};class Car : Vehicle {public:void DoSth()};then just use:Vehicle * v = new Car;v->DoSth();without need of conditions.Kasya   ;Quote:my question is, what is the c++ equivalent of "if (vehicle is Car)" ?typeid or dynamic_cast.The example you have provided, however, is badly designed code regardless of the language. It's also exceedingly academic, and so I suspect it isn't the actual problem you need to solve. However, it's very possible that the code you're trying to port is poorly designed and you should improve it rather than attempt to port it directly.What are you actually trying to do?   ;  Thanks for the replies.The example was contrived for simplicity.I have a linked list of Controls, which may be EditBoxes or ComboBoxes or TextInputs or whatever.The virtual function technique does work in many situations, but sometimes i'd like to iterate through the list and only call a function if its, say, a ComboBox.I've done this many times before in C#, i never realized it was bad practice!nat.;Quote:Original post by cadmonkeyThe virtual function technique does work in many situations, but sometimes i'd like to iterate through the list and only call a function if its, say, a ComboBox.Make a separate list with just the ComboBoxes in it. Seriously.   ;Quote:Original post by cadmonkeyI've done this many times before in C#, i never realized it was bad practice!c# is not c++.reflection works fine with c#'s type model, so there's really no reason why you should not use it there.in c++ however, things are different. i'll second zahlman's suggestion, just build a second list for iteration.;Visitor pattern.It is intrusive, and hierarchy of types that can be visited needs to be maintained manually.There is an alternative, which looks something like this:typedef map<const char *, boost::function<void(Base*>>> Visitor;struct Base {  virtual ~Base() {}  // force Base to be virtual  void visit(Visitor & v) {    if (!v.contains[typeid(this)]) return; // no need to visit    v[typeid(this)](this);  }};The visit function is not polymorphic, so it could potentially be used with template parameter.The map is somewhat tricky to configure, since it requires finding proper function pointer type, preferably one that supports mapping of concrete types, so that something like this can be done:void doComboBox(ComboBox * ptr);visitors[typeid(ComboBox)] = doComboBox;This avoids the need for casting in visitor function, but may require some forced casts via templates during construction of map.The rationale for this approach is that visitors are constructed at call site, rather than via external interface. If templates are used, then type lists can be a visitor, or a tuple, or custom class.It's a bit messy and I really don't feel like wrangling function pointer syntax, so the above is mostly pseudo code. Some caution must also be taken when dealing with typeid.If given a choice, I'd prefer separate list.   