C++多态性:从父类到子类

use*_*982 8 c++ oop polymorphism

在C++中我们可以将子类指针转换为父类,但是有没有办法将它转换回来:从父类获得,从子类获得,给孩子类回来?

我的意思是:

class Parent
{
    ...
};

class Child : public Parent
{
    ...
};

int main(int argc, char const *argv[])
{
    Child* child = new Child();
    Parent* parent = child;
    Child* old_child = parent; // how to do this??
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的回答.

πάν*_*ῥεῖ 11

"但是有什么方法可以将它转换回来:从父母那里获得,这是从孩子那里获得的,给孩子上课了吗?"

是的,正如其他答案中所提到的,有两种方法可以做到这一点.

Child * old_child = dynamic_cast<Child*>(parent);
Run Code Online (Sandbox Code Playgroud)

dynamic_cast<>可以在运行时检查结果,因此您可以确定parent对象是否真正代表Child实例:

if(!old_child) {
     // parent is not a Child instance
}
Run Code Online (Sandbox Code Playgroud)

还要注意为了使其正常工作,所讨论的类需要有一个vtable,RTTI实际上可以确定它们的关系.实现此目的的最简单形式是为Parent类提供虚拟析构函数

class Parent {
public:
    virtual ~Parent() {}
    // or
    // virtual ~Parent() = default;
    // as suggested for latest standards
};
Run Code Online (Sandbox Code Playgroud)

注意:
如果这应该适用于一般设计决定,我会强烈忽视它.使用纯虚拟接口,保证是否实现.


第二种方式static_cast<>可以在环境中使用,你知道它parent实际上是一个孩子.最简单的形式是CRTP,其中Parent继承类作为模板参数

template <class Derived>
class Parent {

     void someFunc() {
         static_cast<Derived*>(this)->doSomething();
     }
};

class Child : public Parent<Child> {
public:
    void doSomething();
};
Run Code Online (Sandbox Code Playgroud)

在编译时检查Parent<>static_cast<>检查的有效性.

注意:
另一个优点是您可以使用一个使用的派生接口

  • 静态类成员 Derived
  • typedef由...提供 Derived
  • ...更多类特征,可以在编译时检查