函数调用和参数传递中涉及的概念
concept involved here in function calling and argument passing
在阅读vptr和vtable概念时,我得到了一段精彩的代码,但我无法理解这里涉及的概念:
#include <iostream>
using namespace std;
class A
{
public:
virtual void foo(int x = 10)
{
cout << "base x : " << x << "n";
}
virtual void bar()
{
cout << "base barn";
}
};
class B : public A
{
public:
virtual void foo(int x = 20)
{
cout << "derived x : " << x << "n";
}
private:
virtual void bar()
{
cout << "derived barn";
}
};
class C : public B
{
};
int main()
{
A x; x.foo(); // x.foo(10);
B y; y.foo(); // x.foo(20);
A *p(&y);
p->foo();
}
现在我得到的输出是:
base x : 10
derived x : 20
derived x : 10
即使在打印派生x(即B::foo())时,默认参数也是基函数(即A::foo)的参数,这怎么可能呢?
C++标准第8.3.6节第10点提到:
虚拟函数调用(10.3)使用由的静态类型确定的虚拟函数的声明表示对象的指针或引用。覆盖函数在派生类中,不从它覆盖的函数。
在您的示例中,默认参数的评估是基于"p"类型(即"A")进行的。因此,默认参数的计算是从A的声明中完成的,函数的调用是通过vptr表中的常规查找进行的。
默认参数似乎是在编译时解析的。看看这里和这里。
使用的默认值将是在静态(编译时)类型中定义的值。因此,如果要更改重写中的默认参数,但通过基类指针或引用调用函数,则将使用基类中的默认值。
相关文章:
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 可变参数模板函数:调用没有匹配函数,std::endl
- 如何在类(C )中作为参数传递函数
- 将基类 2 参数构造函数调用到子类 1 参数构造函数中
- 功能参数与函数调用不匹配
- 参数构造函数调用调用
- 对象将不带参数的函数调用到需要参数的函数
- 是在将对象作为参数传递时调用的构造函数
- 用函数作为模板参数的C++函数调用包装器
- Visual Studio 2015 错误 C4996"std::_Copy_impl":使用可能不安全的参数进行函数调用
- 为什么在将对象作为参数传递时调用析构函数而不调用构造
- 为什么我们不能从具有默认参数的函数调用函数?
- 使用 Google C++ 模拟框架 (Google Mock) (V1.5) 将任意参数传递给调用的方法
- 如何让复制构造函数通过可变参数构造函数调用
- 结构参数在函数调用时损坏
- 是具有单个非类型模板参数参数的函数调用表达式
- 在 C++ 中将命令行参数作为函数调用的简单方法
- 错误 C4996:"std::_Copy_impl":使用可能不安全的参数进行函数调用
- 递归可变参数模板函数调用第二个参数类型的指针"loses"指针
- C++可变参数模板函数调用应用了什么模式匹配(如果有)