简体   繁体   English

如何将派生类与向量和共享指针一起使用?

[英]How shall I use derived classes with vectors and shared pointers?

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. 我有2个课程,Head和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 我认为最好的实现方法是创建一个类BodyPart,像这样

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: 最后,我想声明一个BodyPart元素的向量:

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

containing either Hand or Head elements, and call my methods on the vector elements when I need. 包含Hand或Head元素,并在需要时在vector元素上调用我的方法。

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. 显然,当我尝试获取向量的元素时,编译器抱怨它无法从BodyPart共享指针转换为Hand共享指针。 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. 而且,如果向量是像上面那样声明的,即使它实际上来自该类,我也不能在其元素上调用特定于派生类的方法(例如getNumFinger())。

Is there a proper way to deal with this? 有解决这个问题的适当方法吗? Or is my approach completely wrong? 还是我的方法完全错误? Thanks in advance! 提前致谢!

Your approach is correct, but so is the compiler. 您的方法是正确的,但编译器也是如此。 It's good that you have a vector of shared pointers to the base class, but you'll need to cast to the actual type to get specific functionality. 拥有指向基类的共享指针向量是很好的,但是您需要强制转换为实际类型以获取特定功能。

The typedef boost::shared_ptr<Hand> pointer; typedef boost::shared_ptr<Hand> pointer; is useless though, you only need the typedef in the base class. 虽然没有用,但您只需要基类中的typedef You won't be casting between smart pointers. 您不会在智能指针之间进行转换。 I'd rename the base class typedef to 我将基类typedef重命名为

typedef boost::shared_ptr<BodyPart> BodyPartPtr;

though. 虽然。

Good point by Alan in the comments: you should make the base class's destructor virtual to prevent UB, since you'll be deleting (indirectly) pointers to derived types through a pointer to a base type. 艾伦(Alan)在评论中指出了一点:您应该使基类的析构函数虚拟化以防止UB,因为您将通过指向基类型的指针(间接)删除指向派生类型的指针。

Your approach is right (though you don't really need the typedef ), you just need to use boost::static_pointer_cast ( Doc here ) to convert from shared_ptr<BodyPart> to shared_ptr<Hand> when you need it. 您的方法是正确的(尽管您实际上并不需要typedef ),您只需要使用boost::static_pointer_cast (在Doc这里 ),在需要时从shared_ptr<BodyPart>转换为shared_ptr<Hand>

You may also have a look at enable_shared_from_this at some point in your learning. 在您学习的某些时候,您还可以查看enable_shared_from_this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM