当前位置:网站首页>一知半解 ~题目杂记 ~ 一个多态问题
一知半解 ~题目杂记 ~ 一个多态问题
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 还会是父类在栈中保存的值!!!!
边栏推荐
- Axurerp prototype design starts quickly
- 存算一体与存内计算计算杂谈
- Discussion on the integration of storage and calculation and the calculation in storage
- The use and Simulation of string function, character function and memory function
- 基于C语言实现的NFA确定化和DFA最小化
- 网络安全(5)
- Trie树(字典树)讲解
- 使用cpolar发布树莓派网页(cpolar隧道的完善)
- 36. JS animation
- Study and exploration of Redux API implementation of Redux
猜你喜欢

当 update 修改数据与原数据相同时会被更新吗?

NFA determination and DFA minimization based on C language

数据库表结构生成excel工具

Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in

分布式Session共享的4类技术方案,与优劣势比较

网络安全(6)

Flowable UI制作流程图

Introduction to translation professional qualification (level) examination

MySQL的数据类型

VS2015采用loadlibrary方式调用dll库
随机推荐
[Yunzhu co creation] [hcsd live broadcast] teach the interview tips of big companies in person
Unity3d hodgepodge
当 update 修改数据与原数据相同时会被更新吗?
PyQt5快速开发与实战 6.5 QGridLayout(网格布局)
How to change MySQL into Chinese
On the charm of code language
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
【BERT-多标签文本分类实战】之一——实战项目总览
Study and exploration of Redux API implementation of Redux
Opencv introductory basic learning
What is the key to fast multi tag user profile analysis?
mysql将部分表名统一转换为大写
Data representation and calculation (base)
VS2015采用loadlibrary方式调用dll库
Summary of some experiences in the process of R & D platform splitting
Use cpolar to publish raspberry pie web pages (improvement of cpolar function)
Webassembly 2022 questionnaire results are fresh
40余岁的边缘老技术,是未来乏力还是掘地而起?
Random number setting and reference between parameters
数仓项目踩坑记录与解决方法总结