为什么运行时多态性不能在编译时解决? [英] Why can't run-time polymorphism be solved at compile time?

查看:165
本文介绍了为什么运行时多态性不能在编译时解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑:

#include<iostream>
using namespace std;

class Base
{
    public:
        virtual void show() { cout<<" In Base \n"; }
};

class Derived: public Base
{
    public:
       void show() { cout<<"In Derived \n"; }
};

int main(void)
{
    Base *bp = new Derived;
    bp->show();  // RUN-TIME POLYMORPHISM
    return 0;
}

为什么这段代码导致运行时多态性,为什么不能解决因为在一般情况下,在编译时是不可能在编译时确定什么?

Why does this code cause run-time polymorphism and why can not it be solved at compile time?

推荐答案

类型它将在运行时。您的示例可以在编译时解决(请参阅@Quentin的解答),但是可以构造不能的案例,例如:

Because in the general case, it's impossible at compile time to determine what type it will be at run time. Your example can be resolved at compile time (see answer by @Quentin), but cases can be constructed that can't, such as:

Base *bp;
if (rand() % 10 < 5)
    bp = new Derived;
else
    bp = new Base;
bp->show(); // only known at run time

编辑:感谢@nwp,这里有一个更好的例子。像:

Thanks to @nwp, here's a much better case. Something like:

Base *bp;
char c;
std::cin >> c;
if (c == 'd')
    bp = new Derived;
else
    bp = new Base;
bp->show(); // only known at run time 

此外,通过测试证明,可以显示在一般情况下 在数学上,C ++编译器不可能知道基类指针指向什么

Also, by corollary of Turing's proof, it can be shown that in the general case it's mathematically impossible for a C++ compiler to know what a base class pointer points to at run time.

假设我们有一个类似C ++编译器的函数:

Assume we have a C++ compiler-like function:

bool bp_points_to_base(const string& program_file);

其输入 program_file : C ++源代码文本文件的名称,其中指针 bp (如在OP中)调用其 virtual 成员函数 show()。并且可以在一般情况下确定 (在序列点 A ,其中 virtual show()首先通过 bp 调用:是否指针 bp 指向 Base 的实例。

That takes as its input program_file: the name of any C++ source-code text file where a pointer bp (as in the OP) calls its virtual member function show(). And can determine in the general case (at sequence point A where the virtual member function show() is first invoked through bp): whether the pointer bp points to an instance of Base or not.

考虑下面的C ++程序片段q .cpp:

Consider the following fragment of the C++ program "q.cpp":

Base *bp;
if (bp_points_to_base("q.cpp")) // invokes bp_points_to_base on itself
    bp = new Derived;
else
    bp = new Base;
bp->show();  // sequence point A

现在如果 bp_points_to_base 确定在q.cpp中: bp 指向 Base 的实例 A ,然后q.cpp指向 A 处的 bp 如果它确定在q.cpp: bp 没有指向 Base 的实例 A ,则q.cpp将 bp 指向 Base A 。这是一个矛盾。所以我们的初步假设是不正确的。因此 bp_points_to_base 不能写入一般情况。

Now if bp_points_to_base determines that in "q.cpp": bp points to an instance of Base at A then "q.cpp" points bp to something else at A. And if it determines that in "q.cpp": bp doesn't point to an instance of Base at A, then "q.cpp" points bp to an instance of Base at A. This is a contradiction. So our initial assumption is incorrect. So bp_points_to_base can't be written for the general case.

这篇关于为什么运行时多态性不能在编译时解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆