简体   繁体   English

使用dynamic_cast和多态的最佳实践

[英]Best practice with dynamic_cast and polymorphism

I have a design problem that I am not sure how to handle in the best way. 我有一个设计问题,我不知道如何以最好的方式处理。 I want my code to be future proof and still not be to messy and complex (the plight of a geek). 我希望我的代码能够成为未来的证明,而且仍然不会变得混乱和复杂(极客的困境)。

Currently my design has the following setup 目前我的设计有以下设置

derived class D          derived class E
  ^                          ^
  |                          |
derived abstract class B  ->              derived class C
  ^                                            ^ 
  |                                            |
              Abstract Base class A

class B differs from class A , class D and class E differs from class B and C differs from class A . B类与A类不同, D类和E类与B类不同, CA类不同。

As these classes differ I need to use the dynamic_cast operator. 由于这些类不同,我需要使用dynamic_cast运算符。

A* a1 = new class C;
C* c1 = dynamics_cast<C*>(a1);
if(c1){ //Success!!!}
...

As you can see I will be wanting to use dynamic_cast a lot! 正如您所看到的,我将非常想要使用dynamic_cast I need to try and find a nice way of doing this that is not full of if() statements extra. 我需要尝试找到一个很好的方法来完成这个不完整的if()语句。

Is there a neat way of doing this? 这样做有一个简洁的方法吗? I seem to remember seeing something in a Scott Meyers book, but I am away from home and can not access it for a week or two. 我似乎记得在Scott Meyers的书中看到了一些东西,但我不在家,一两个星期都无法访问它。

Any references/examples would be much appreciated. 任何参考/示例将非常感激。

Also if need be I could make class B into an interface which would be a sort of solution 此外,如果需要,我可以将B类变成一个接口,这将是一种解决方案

B* d1 = new D;
B* e1 = new E;

but not ideal. 但并不理想。

NOTE: I have added this example to show my current design failure 注意:我已添加此示例以显示我当前的设计失败

class A {...};

class B: public A {
   public:
      virtual std::vector<objectType1*>& getObjectType1();
};

class C: public B {
   private:
     void newFunction();
};

A* c1 = new C();
std::vector<objectType1*> example = c1->getObjectType1(); // Wrong; need dyanmic_cast :-(

can not access getObjectType1() without dynamic_cast as the static type A does not know about this function. 如果没有dynamic_cast ,则无法访问getObjectType1() ,因为静态类型A不知道此函数。 I need to rethink my design. 我需要重新考虑我的设计。 Any ideas? 有任何想法吗?

In a properly designed object hierarchy, the use of dynamic_cast is a fair rarity. 在正确设计的对象层次结构中,使用dynamic_cast是一种罕见的事情。 Sure, it exists to cast up & down a polymorphic tree, but that doesn't mean you have to use it. 当然,存在抛出多态树,但这并不意味着你必须使用它。

Think about this. 想想这个。 Why would you need to use dynamic_cast , generally speaking? 一般来说,为什么你需要使用dynamic_cast Because the base pointer you have doesn't provide the facilities you need, right? 因为您拥有的基准指针不能提供您需要的设施,对吧? And what is the point of polymorphism? 多态性有什么意义? To provide different behavior for similar operations, where the behavior is chosen at run-time, right? 为类似操作提供不同的行为,在运行时选择行为,对吧?

Aren't those two statements somewhat contradictory? 那两个陈述有点矛盾吗? If your object's interface is designed correctly, then you don't really care what kind of behavior is going to be employed when you call ->Foo() -- you just want to call ->Foo() . 如果您的对象的界面设计正确,那么当您调用时,您并不关心将采用什么样的行为->Foo() - 您只想调用->Foo()

If you need to make frequent use of dynamic_cast , it's a code smell that tells you that there's something wrong with your interfaces. 如果你需要经常使用dynamic_cast ,那么代码味道会告诉你接口有问题。 Maybe you're trying to shove too much in to one interface? 也许你试图在一个界面上推得太多?

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

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