Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:18:09+00:00 2026-06-17T05:18:09+00:00

I asked a couple days ago some clarifications on inheritance , a concept I

  • 0

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
{
  public:
  typedef boost::shared_ptr<BodyPart> BodyPartPtr;

  BodyPart();
  virtual ~BodyPart();

  private:
  int commonMember1;
  double commonMember2;

  public:
  int commonMethod1();
  int CommonMethod2();
}

while Hand is something like this:

class Hand : public BodyPart
{
  public:
  Hand();
  ~Hand();

  private:
  int numFingers;
  double otherVar;

  public:
  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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:18:11+00:00Added an answer on June 17, 2026 at 5:18 am

    EDIT: I have augmented the examples to make them clearer.

    Relaying on casts is usually a sign of a bad design. Casts have their place, but this does not look to be it.

    You need to ask yourself what do you want to do with the objects stored in cBodyParts. For sure, you will be doing different things with a Hand or with a Head, but you can probably abstract them somehow: this is what virtual functions do. So, in addition to what you have already written for your classes, you would just need an additional virtual function in them:

    class BodyPart
    {
      // Same as you wrote, plus:
    public:
      virtual void InitialisePart() = 0; // Pure virtual: each body part must say how to process itself
      virtual void CalibrateJoints() {} // Override it only if the body part includes joints
    }
    
    class Head : public BodyPart
    {
      // Same as you wrote, plus:
    public:
      virtual void InitialisePart() {
        // Code to initialise a Head
      }
      // Since a Head has no joints, we don't override the CalibrateJoints() method
    }
    
    class Hand : public BodyPart
    {
      // Same as you wrote, plus:
    public:
      virtual void InitialisePart() {
        // Code to initialise a Hand
      }
      virtual void CalibrateJoints() {
        // Code to calibrate the knuckles in the hand
      }
    }
    

    And then you no longer need any casts. For instance:

    for (BodyPart::BodyPartPtr part : cBodyParts) {
      part->InitialisePart();
      part->CalibrateJoints(); // This will do nothing for Heads
    }
    

    As you can see, no casts at all and everything will work fine. This scheme is extensible; if you later decide that you need additional classes inheriting from BodyPart, just write them and your old code will work correctly:

    class Torso : public BodyPart
    {
    public:
      virtual void InitialisePart() {
        // Code to initialise a Torso
      }
      // The Torso has no joints, so no override here for CalibrateJoints()
    
      // Add everything else the class needs
    }
    
    class Leg : public BodyPart
    {
    public:
      virtual void InitialisePart() {
        // Code to initialise a Leg
      }
      virtual void CalibrateJoints() {
        // Code to calibrate the knee
      }
    
      // Add everything else the class needs
    }
    

    Now you don’t need to change the code you wrote previously: the for loop above will work correctly with and Torso or Leg it finds with no need for an update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've asked a question here couple days ago, about how to avoid a function
I asked a question a couple days ago about creating INSERTs by running a
A couple of days ago I asked a question about how to replace and
I asked a similar question a couple of days ago, but I'm looking for
This is similar to a question I asked a couple of days ago. However,
I asked a question a couple of days ago regarding installing numpy on the
I asked this question a couple days ago but I think I worded my
I asked a question a couple of days ago about populating a listbox on
I asked a question here a couple weeks ago about getting sticky headers to
So i asked a couple of days ago Here as to which charts one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.