当前位置:网站首页>一知半解 ~题目杂记 ~ 一个多态问题
一知半解 ~题目杂记 ~ 一个多态问题
2022-07-29 09:29:00 【小强~】
题代码
class A
{
public:
virtual void func(int val = 1){
std::cout << "A->" << val << std::endl; }
virtual void test(){
func(); }
};
class B : public A
{
public:
void func(int val = 0){
std::cout << "B->" << val << std::endl; }
};
int main(int argc, char* argv[])
{
B* p = new B;
p->test();
p->func();
return 0;
}
运行结果:

解释:
子类的重写虚函数会覆盖自己虚表中父类对应虚函数的对应虚函数指针,虚表是在代码段,但此时调用的父类中的test()时,其val的值为1,所以这个题会先调用子类虚表中对应的虚函数指针,执行该虚函数,但其中调用的 val 还会是父类在栈中保存的值!!!!
边栏推荐
- Pyqt5 rapid development and practice 6.4 qboxlayout (box layout)
- 常用的DOS命令[逐渐完善]
- "Defects" of prototype chain inheritance and constructor inheritance
- 36. JS动画
- Custom configuration
- 数据库表结构生成excel工具
- Flowable 基础篇1
- Parameter passing mode of C language (int x) (int *x) (int & x)
- Summary of research on endogenous information security technology of industrial measurement and control equipment
- Using logistic regression and neural network to deal with complex binary classification problems
猜你喜欢

Appendix 2 – some simple exercises

附录2-一些简单的练习
![[machine learning] logistic regression code exercise](/img/dc/203f240e2eb213dbd6173d17e47ec6.jpg)
[machine learning] logistic regression code exercise

User identity identification and account system practice

PyQt5快速开发与实战 6.4 QBoxLayout(框布局)

Vs2015 uses loadlibrary to call DLL library

OpenCV入门基础学习
![Acwing game 59 [End]](/img/a6/70d76e78e49dc2ad08084f58750017.png)
Acwing game 59 [End]

I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime

AxureRP原型设计 快速开始
随机推荐
Excellent package volume optimization tutorial
40余岁的边缘老技术,是未来乏力还是掘地而起?
远程连接windows版本服务器redis的配置文件设置
[Bert multi label text classification practice] I - overview of practical projects
How to contribute to openharmony
WebAssembly 2022 问卷调查结果新鲜出炉
Unity xchart3.0 basic usage quick start
Using logistic regression and neural network to deal with complex binary classification problems
先序遍历/后序遍历确定树的大致形态
MySQL converts some table names to uppercase
The use and Simulation of string function, character function and memory function
数据库表结构生成excel工具
Evaluation index of machine learning classification model and implementation of sklearn code
Flowable 基础篇2
核酸扫码登记体验有感(如何提高OCR的文字正确识别率)
Gao Zhiwei: data management enables the digital transformation of the transportation industry
OpenCV入门基础学习
[Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed
查看端口占用情况
浅谈契约测试