简体   繁体   English

c ++继承和共享指针

[英]c++ Inheritance and shared pointers

Here is the situation. 这是情况。 Let's say we have a virtual base class (eg ShapeJuggler ) which contains a method that takes a shared pointer to a virtual base class object (eg Shape ) as argument. 假设我们有一个虚拟基类(例如ShapeJuggler ),其中包含一个方法,该方法将指向虚拟基类对象(例如Shape )的共享指针作为参数。 Let's jump into the following pseudo-code to understand: 让我们跳入以下伪代码以了解:

class Shape {
}
class ShapeJuggler {
    virtual void juggle(shared_ptr<Shape>) = 0;
}
// Now deriving a class from it
class Square : public Shape {
}
class SquareJuggler : public ShapeJuggler {
public:
    void juggle(shared_ptr<Shape>) {
         // Want to do something specific with a 'Square'
         // Or transform the 'shared_ptr<Shape>' into a 'shared_ptr<Square>'
    }
}
// Calling the juggle method
void main(void) {
    shared_ptr<Square> square_ptr = (shared_ptr<Square>) new Square();
    SquareJuggler squareJuggler;
    squareJuggler.juggle(square_ptr); // how to access 'Square'-specific members?
}

make_shared or dynamic/static_cast don't seem to do the job. make_shared或dynamic / static_cast似乎不起作用。 Is it at all possible? 有可能吗? Any ideas, suggestions? 有什么想法,建议吗?
Thanks 谢谢

This is where std::dynamic_pointer_cast (or one of its friends) comes into play. 这就是std::dynamic_pointer_cast (或其朋友之一)开始起作用的地方。
It's just like dynamic_cast , but for std::shared_ptr s. 就像dynamic_cast一样,但是对于std::shared_ptr

In your case (assuming the Shape class is polymorphic so dynamic_cast works): 在您的情况下(假设Shape类是多态的,那么dynamic_cast可以工作):

void juggle(shared_ptr<Shape> shape) {
    auto const sq = std::dynamic_pointer_cast<Square>(shape);
    assert(sq);

    sq->squareSpecificStuff();
}

This is the multiple dispatch problem. 这是多重调度问题。 Their are many solution to this problem, the cleanest might be using the visitor pattern , but if you just have one function that need multiple dispatch you could avoid using a visitor: 他们有很多解决此问题的方法,最干净的方法可能是使用visitor模式 ,但是如果您只有一个需要多次调度的功能,则可以避免使用visitor:

class SquareJuggler;
class TriangleJuggler;
//.... others concrete jugglers.

class Shape {
    //The default behaviour for any juggler and any shape
    virtual void juggle_by(Juggler& t) {
       //default code for any shape an juggle
    }
    // list each juggler for which you may
    // implement a specific behavior
    virtual void juggle_by(SquareJuggler& t) {
      //provides default behavior in case you will not
      // create a specific behavior for a specific shape.
      //for example, just call the unspecific juggler:
      this->Shape::juggle_by(static_cast<Juggler&>(t));
    }
    virtual void juggle_by(TriangleJuggler& t) {
      //provides default behavior in case you will not
      //create a specific behavior for a specific shape.
      //for example, just call the unspecific juggler:
      this->Shape::juggle_by(static_cast<Juggler&>(t));
    }
   //...
};
// Now deriving a class from it
class Square : public Shape {
  void juggle_by(SquareJuggler& s) override{
     //code specific to SquareJuggler and Shape
  }
};
class Triangle : public Shape {
  void juggle_by(TriangleJuggler& t) override{
     //code specific to TriangleJuggler and Shape
  }
};

class ShapeJuggler {
  virtual void juggle(shared_ptr<Shape> s) {
    //by default (if default has sense):
    s->juggle_by(*this);
  }
};
class SquareJuggler: public ShapeJuggler {
public:
  void juggle(shared_ptr<Shape> s) override {
      s->juggle_by(*this);
  }
};
class TriangleJuggler: public ShapeJuggler {
public:
  void juggle(shared_ptr<Shape> s) override {
      s->juggle_by(*this);
  }
};
// Calling the juggle method
void main(void) {
    shared_ptr<Square> square_ptr = (shared_ptr<Square>) new Square();
    SquareJuggler squareJuggler;
    squareJuggler.juggle(square_ptr); 
       //This last call, will perform two virtual calls:
       //   1. SquareJuggler::juggle(shared_ptr<Shape);
       //   2. Square::juggle_by(SquareJuggler&);
}

You could also defines your XXXJuggler as final, which will enable some devirtualization optimization. 您也可以将XXXJuggler定义为final,这将实现一些虚拟化优化。

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

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