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

问题描述

请考虑:

 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.

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

09-19 04:55