- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
最近我看到了这个 C++ 标准段落(http://eel.is/c++draft/expr.post#expr.call-5):
If the postfix-expression designates a destructor, the type of the function call expression is void; otherwise, the type of the function call expression is the return type of the statically chosen function (i.e., ignoring the virtual keyword), even if the type of the function actually called is different. This return type shall be an object type, a reference type or cv void.
这部分我不太明白:
the type of the function call expression is the return type of the statically chosen function (i.e., ignoring the virtual keyword), even if the type of the function actually called is different.
even if the type of the function actually called is different.
调用表达式如何实际调用所选择的不同类型的函数?
最佳答案
虚函数可以有协变的返回类型,
所以
struct Base
{
virtual ~Base() = default;
virtual Base* Clone() const { return new Base(*this); }
};
struct Derived : Base
{
// covariant return type:
Derived* Clone() const override { return new Derived(*this); }
};
然后
Derived d;
Base& b = d;
auto* clonePtr = b.Clone(); // `auto` is `Base`, even if `Derived::Clone` is called.
// runtime type of `clonePtr` is `Derived`
std::unique_ptr<Base> clone(clonePtr); // Done in 2 steps for explanation
关于c++ - 静态选择函数和虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52423205/
我有一个特别的问题想要解决,我不确定是否可行,因为我找不到任何信息或正在完成的示例。基本上,我有: class ParentObject {}; class DerivedObject : publi
在我们的项目中,我们配置了虚 URL,以便用户可以在地址栏中输入虚 URL,这会将他们重定向到原始 URL。 例如: 如果用户输入'http://www.abc.com/partner ',它会将它们
我是一名优秀的程序员,十分优秀!