当前位置:网站首页>A little knowledge ~ miscellaneous notes on topics ~ a polymorphic problem
A little knowledge ~ miscellaneous notes on topics ~ a polymorphic problem
2022-07-29 09:42:00 【Xiaoqiang~】
Question code
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;
}
Running results :
explain :
The rewritten virtual function of a subclass will overwrite the corresponding virtual function pointer of the corresponding virtual function of the parent class in its virtual table , The virtual table is in the code segment , But in the parent class of the call at this time test()
when , Its val
The value of is 1, So this problem will first call the corresponding virtual function pointer in the subclass virtual table , Execute the virtual function , But the invoked val
It will also be the value saved by the parent class in the stack !!!!
边栏推荐
- Configuration file settings for remote connection to Windows version server redis
- WebAssembly 2022 问卷调查结果新鲜出炉
- Cloud native management practice: business led Devops continuous delivery system
- 【C语言】三子棋(智能下棋 + 阻拦玩家)
- C# 值类型和引用类型讲解
- Is the marginal old technology in its 40s weak in the future or rising from the ground?
- Data type of MySQL
- (Video + graphics) introduction to machine learning series - Chapter 1 Introduction
- Fluent text editor
- i. Mx6ull driver development | 32 - manually write a virtual network card device
猜你喜欢
不用Swagger,那我用啥?
乱打日志的男孩运气怎么样我不知道,加班肯定很多
使用cpolar发布树莓派网页(cpolar功能的完善)
怎么样的框架对于开发者是友好的?
vector实现
一知半解 ~题目杂记 ~ 一个多态问题
Virtual machines use host graphics cards (Hyper-V and wsl2)
Unity xchart3.0 basic usage quick start
远程连接windows版本服务器redis的配置文件设置
Use cpolar to publish raspberry pie web pages (improvement of cpolar function)
随机推荐
数仓项目踩坑记录与解决方法总结
Pytest+allure generate test report
Vs2015 uses loadlibrary to call DLL library
40余岁的边缘老技术,是未来乏力还是掘地而起?
一知半解 ~题目杂记 ~ 一个多态问题
Study and exploration of Redux API implementation of Redux
STM32 application development practice tutorial: getting to know STM32 for the first time
[Yunzhu co creation] [hcsd live broadcast] teach the interview tips of big companies in person
Qmainwindow details
Excel tool for generating database table structure
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
核酸扫码登记体验有感(如何提高OCR的文字正确识别率)
用户身份标识与账号体系实践
36. JS animation
"Defects" of prototype chain inheritance and constructor inheritance
Explanation of trie tree (dictionary tree)
Div horizontal arrangement
PyQt5快速开发与实战 6.5 QGridLayout(网格布局)
PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局
Source code analysis of senparc.weixin.sample.mp