Converting objects of base class to derived class


I asked a couple days ago some clarifications on inheritance, a concept I am still trying to understand. Here is the follow up question, since I am still facing problems.

In my project I have 2 types of objects, Hand and Face, both inheriting from the base class BodyPart. BodyPart is something like this:

class BodyPart
  typedef boost::shared_ptr<BodyPart> BodyPartPtr;

  virtual ~BodyPart();

  int commonMember1;
  double commonMember2;

  int commonMethod1();
  int CommonMethod2();

while Hand is something like this:

class Hand : public BodyPart

  int numFingers;
  double otherVar;

  int getNumFingers();
  void printInfo();

I also have a vector of BodyPart elements

std::vector<BodyPart::BodyPartPtr> cBodyParts;

composed of Hand or Head objects. In the previous question I was told that this approach makes sense, I just had to cast from the base class to the derived using boost static_pointer_cast

Now, the problem now is that for some of the objects in the vector I don't know whether they are Hand or Head, so at some point in my code I can have in cBodyParts some Hand elements, some Head elements as well as some BodyPart elements. After some further analysis I am able to correctly classify the latter as either Hand or Head and modify accordingly the elements in the vector, but I have no idea on how to make it. Shall I just delete the case class element and create a derived one with the same property? Shall I just avoid inheritance in case like this?

Thanks in advance for the help


As Kerrek SB pointed out this is not feasible at all, but for the sake of answering the actual question, dynamic_cast is what you are looking for.

By : filmor

Use virtual functions, they will simplify a lot your problem.

Else, you can add some methods to distinguish between different types. However, do it only if you cannot do it another way, ie if you cannot do it via virtual functions.

Example 1:

// in BodyPart; to be reimplemented in derived classes
virtual bool isHand() const { return false; }
virtual bool isHead() const { return false; }

// in Hand (similar to what will be in Head)
bool isHand() const { return true; }

// How to use:
BodyPart::pointer ptr = humanBodyVector[42]; // one item from the array
By : Synxis

The hip bone's connected to the thigh bone...

I take it you have some composite of all the body parts, maybe a Body class.

What do you want the body to do?

  • Render itself
  • Serialise
  • Ouput its volume, or bounding box, or some other metric
  • Re-orient itself in response to input
  • Respond to an inverse-kinematic physical model

The list could probably go on. If you know exactly what you want the Body to do you can put that function in the BodyPart base class, and have Body iterate over the composite hierarchical structure of all the connected body parts, calling render, for example.

An alternative is to use a Visitor, which is effectively a way of dynamically adding methods to a static inheritance hierarchy.

This video can help you solving your question :)
By: admin