作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我对以下代码有一个奇怪的情况。请帮我澄清一下。
class B
{
public:
B();
virtual void print(int data=10)
{
cout << endl << "B--data=" << data;
}
};
class D:public B
{
public:
D();
void print(int data=20)
{
cout << endl << "D--data=" << data;
}
};
int main()
{
B *bp = new D();
bp->print();
return 0;
}
关于我期望的输出
[ D--data=20 ]
但实际上是这样
[ D--data=10 ]
请帮忙。这对您来说似乎很明显,但我不知道内部机制。
最佳答案
标准说 (8.3.6.10):
A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.
这意味着,由于您通过 B
类型的指针调用 print
,因此它使用 B::print
的默认参数。
关于c++ - 虚函数默认参数行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44835816/
我有一个特别的问题想要解决,我不确定是否可行,因为我找不到任何信息或正在完成的示例。基本上,我有: class ParentObject {}; class DerivedObject : publi
在我们的项目中,我们配置了虚 URL,以便用户可以在地址栏中输入虚 URL,这会将他们重定向到原始 URL。 例如: 如果用户输入'http://www.abc.com/partner ',它会将它们
我是一名优秀的程序员,十分优秀!