当前位置:网站首页>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 !!!!
边栏推荐
- 【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
- Detailed explanation: what is the GPS Beidou time service server?
- vector实现
- Implementation of DFA string recognition based on C language simulation
- Sublime Text3 set different indents for different files
- Unity xchart3.0 basic usage quick start
- 用户身份标识与账号体系实践
- 原型链继承和构造函数继承的 “毛病”
- 【BERT-多标签文本分类实战】之一——实战项目总览
- Custom configuration
猜你喜欢

先序遍历/后序遍历确定树的大致形态

Basic operations of OpenCV image processing

Unity Xchart3.0基本用法快速上手

Explanation of C value type and reference type

redis可视化工具读取数据乱码问题解决

《LOL》从代码上来说最难的是哪个英雄?

Anfulai embedded weekly report no. 273: 2022.07.04--2022.07.10
![[苹果开发者账号]06 转让开发者账号后,开发者年费自动续费问题](/img/a7/12fd63f16ebca81a3dd2d1b97847d1.png)
[苹果开发者账号]06 转让开发者账号后,开发者年费自动续费问题

Cloud native management practice: business led Devops continuous delivery system

Discussion on the integration of storage and calculation and the calculation in storage
随机推荐
What kind of framework is friendly to developers?
如何为OpenHarmony做贡献
Anfulai embedded weekly report no. 273: 2022.07.04--2022.07.10
基于ArkUI eTS开发的坚果新闻(NutNews)
Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
Fluent text editor
Detailed explanation: what is the GPS Beidou time service server?
怎样查询快递物流筛选出无信息单号删除或者复制
On contract testing
C# 值类型和引用类型讲解
当 update 修改数据与原数据相同时会被更新吗?
36. JS动画
Will the modified data be updated when it is the same as the original data?
2021年CS保研经历(六):系统填报 + 一些感想
查看端口占用情况
[centralized training] hcip cloud computing resource exchange post
mysql将部分表名统一转换为大写
User identity identification and account system practice
How to contribute to openharmony
【BERT-多标签文本分类实战】之一——实战项目总览