【无标题】

复制代码
#include <iostream>
using namespace std;

class Father
{
  public:
    Father() { A(); }
     void virtual A() { cout<<"Father::A()"<<endl; }
     void B() { cout<<"Father::B()"<<endl; A();}
};
class Son : public Father
{
  public:
    void A() { cout<<"Son::A()"<<endl; }
    void B() { cout<<"Son::B()"<<endl; }
};
int main()
{
  Father f;//father::A
  Son s;//father::a!!!
  Father *pf=&s;
  pf->A();
 
  pf->B();
  s.A();//son::a
  s.B();//son::b
}

关注到如果虚函数是在Father()的构造函数中,是不体现虚特性的,而在其它地方是体现的,比如关注void B()中的A()

相关推荐
Z***258012 小时前
Java计算机视觉
java·开发语言·计算机视觉
Tiger_shl12 小时前
SqlConnection、SqlCommand 和 SqlDataAdapter
开发语言·数据库·c#
一点事12 小时前
ruoyi:集成mybatisplus实现mybatis增强
java·开发语言·mybatis
hweiyu0012 小时前
数据结构:数组
数据结构·算法
你的冰西瓜12 小时前
C++14 新特性详解:相较于 C++11 的主要改进
开发语言·c++·stl
linksinke12 小时前
Mapstruct引发的 Caused by: java.lang.NumberFormatException: For input string: ““
java·开发语言·exception·mapstruct·numberformat·不能为空
无限进步_12 小时前
C语言单向链表实现详解:从基础操作到完整测试
c语言·开发语言·数据结构·c++·算法·链表·visual studio
初夏睡觉12 小时前
循环比赛日程表 题解
数据结构·c++·算法
好好研究13 小时前
SpringMVC框架 - 异常处理
java·开发语言·spring·mvc
songroom13 小时前
Rust: 量化策略回测与简易线程池构建(MPMC)
开发语言·后端·rust