当前位置:网站首页>一知半解 ~题目杂记 ~ 一个多态问题
一知半解 ~题目杂记 ~ 一个多态问题
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 还会是父类在栈中保存的值!!!!
边栏推荐
猜你喜欢

MySQL converts some table names to uppercase

Using logistic regression and neural network to deal with complex binary classification problems

Outlook tutorial, how to create an electronic signature in outlook?

Random number setting and reference between parameters

【BERT-多标签文本分类实战】之一——实战项目总览
Jetpack Glance? The spring of widgets is coming

Summary of introduction to unityshader (2): Beginner Level

mysql将部分表名统一转换为大写

On contract testing

Floweable advanced
随机推荐
Floweable advanced
36. JS animation
(视频+图文)机器学习入门系列-第1章 引言
Shutter gradient
Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
PyQt5快速开发与实战 6.5 QGridLayout(网格布局)
Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System
PyQt5快速开发与实战 6.4 QBoxLayout(框布局)
Using logistic regression and neural network to deal with complex binary classification problems
分布式Session共享的4类技术方案,与优劣势比较
How to introduce your project experience?
Unity Xchart3.0基本用法快速上手
Axurerp prototype design starts quickly
Flutter文本编辑器
Handwritten character recognition
Introduction to translation professional qualification (level) examination
机器学习之分类模型评估指标及sklearn代码实现
Outlook tutorial, how to create an electronic signature in outlook?
高智伟:数据管理赋能交通行业数字化转型
数据库表结构生成excel工具