I am not familiar with the concept of inheritance, and I am struggling to find a solution to the following problem.
I have 2 classes, Head and Hand. I use instances of these classes mostly as elements of a vector. The two classes have methods in common and methods peculiar only to them.
Moreover, I deal with shared pointers of the object.
I thought the best way to implement it was to create a class BodyPart, like this
class BodyPart
{
public:
typedef boost::shared_ptr<BodyPart> pointer;
private:
int commonMember1;
double commonMember2;
public:
int commonMethod1();
int CommonMethod2();
}
and the two derived classes like this
class Hand : public BodyPart
{
public:
typedef boost::shared_ptr<Hand> pointer;
private:
int numFingers;
public:
int getNumFingers();
void printInfo();
}
Finally, I wanted to declare a vector of BodyPart elements:
std::vector<BodyPart::pointer> cBodyParts;
containing either Hand or Head elements, and call my methods on the vector elements when I need.
But this approach doesn't seem to work very well. Apparently, when I try to get an element of the vector, the compiler complains that it cannot convert from a BodyPart shared pointer to a Hand shared pointer. Moreover, if the vector is declared like above, I cannot call methods specific to the derived classes (like getNumFinger() ) on its element, even if the actually are from that class.
Is there a proper way to deal with this? Or is my approach completely wrong? Thanks in advance!