简体   繁体   English

C++ 重载一个 Function 基于 shared_ptr 派生的 Class

[英]C++ Overloading a Function Based on shared_ptr Derived Class

There are a lot of SO questions which are similar to this, but I couldn't find precisely what I was looking for.有很多与此类似的 SO 问题,但我无法准确找到我正在寻找的内容。 I'm sorry if this is a duplicate.如果这是重复的,我很抱歉。

I have a Parent class and two derived classes which inherit from it:我有一个Parent class 和两个继承自它的派生类:

class Son : public Parent { ... };
class Daughter : public Parent { ... };

I then declare two shared_ptr to the Base class, and instantiate them with a shared_ptr to each of the derived classes:然后我向 Base class 声明两个shared_ptr ,并使用shared_ptr将它们实例化到每个派生类:

shared_ptr<Parent> son = shared_ptr<Son>(new Son());
shared_ptr<Parent> daughter = shared_ptr<Daughter>(new Daughter());

Lastly, I would like to have a class which processes shared_ptr<Parent> based on which of the derived classes it points to.最后,我想要一个 class 根据它指向的派生类来处理shared_ptr<Parent> Can I use function overloading to achieve this effect is the question:我可以使用 function 重载来达到这个效果的问题是:

class Director {
public:
    void process(shared_ptr<Son> son);
    void process(shared_ptr<Daughter> daughter);
    ...
};

So I would like to be able to write the following code:所以我希望能够编写以下代码:

shared_ptr<Parent> son = shared_ptr<Son>(new Son());
shared_ptr<Parent> daughter = shared_ptr<Daughter>(new Daughter());
Director director;
director.process(son);
director.process(daughter);

Right now I'm having to do (which I would like to avoid):现在我不得不做(我想避免):

director.process_son(boost::shared_polymorphic_downcast<Son>(son));
director.process_daughter(boost::shared_polymorphic_downcast<Daughter>(daughter));

It isn't possible to overload like that, you would need a switch statement to achieve something similar with dispatch within one method on Director .不可能像那样重载,您需要一个 switch 语句来在Director的一个方法中实现与调度类似的东西。

Maybe you should move the process() method to the Parent class (and override it in Son and Daughter ), that way you can use normal virtual function resolution to call the right one.也许您应该将process()方法移动到Parent class (并在SonDaughter中覆盖它),这样您就可以使用普通的虚拟 function 分辨率来调用正确的分辨率。

I am not sure if there is any better ways so i usually use Visitor pattern http://en.wikipedia.org/wiki/Visitor_pattern or similar double dispatch tricks on such need.我不确定是否有更好的方法,所以我通常使用访客模式http://en.wikipedia.org/wiki/Visitor_pattern或类似的双重调度技巧来满足这种需要。

Your objects likely don't know the shared_ptr to themself but usually simple reference works well enough.您的对象可能自己不知道 shared_ptr,但通常简单的引用就足够了。

Ideally, the Parent would provide an interface rich enough for Director to process each child correctly, without the need to know which child it is.理想情况下, Parent会提供一个足够丰富的接口,让Director能够正确处理每个孩子,而无需知道它是哪个孩子。

class Director {
public:
    void process(shared_ptr<Parent> obj);
};

If this for some reason can't be achieved, there are many design options.如果由于某种原因无法实现这一点,则有很多设计选择。 As pointed out above you can have the process linked together with the child class.如上所述,您可以将进程与子 class 链接在一起。 If that doesn't suit your needs you can, for example, isolate the process from the director:如果这不适合您的需求,例如,您可以将流程与主管隔离开来:

class ProcessSon: public Process, private Son {
public:
  void SpecificProc1(shared_ptr<Parent> obj) {
    // Make this part of the process based on class Son
  }

  void SpecificProc2(shared_ptr<Parent> obj) {
    // Make this part of the process based on class Son
  }
  ...
};

Do a similar one for Daughter and send the appropriate Process together with the pointer to the class to the process method of Director .Daughter执行类似的操作,并将相应的Process连同指向 class 的指针一起发送到Directorprocess方法。

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

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