当前位置: 首页 > news >正文

关于C++的隐藏 (hidden),重载(overload),重写(override)小结。

关于隐藏 (hidden)
假如继承以后,子类出现父类同名函数, 即使参数的形式不同, 也会导致父类的函数隐藏, 不参与函数匹配,不能使用。
这个链接讲的不错。https://zhuanlan.zhihu.com/p/575423511

#include <iostream>
using namespace std;
class Vehicle {
public:void accelerate() { cout << "Increasing speed: "; }
};
class Aeroplane : public Vehicle {public:void accelerate(int height) {cout << "Accelerating at a height of: " << height;}
};int main() {Aeroplane plane;plane.accelerate(1000);plane.accelerate();  //这里出错!main.cpp:17:25: error: no matching function for call to ‘Aeroplane::accelerate()’cout << endl;
}

如果子类的instance就是想调用父类的同名不同参的函数呢?方法有两个。

  1. 显式调用父类版本的函数
    b.A::func(); // 显式指定父类版本,合法
    #include
    using namespace std;
    class Vehicle {
    public:
    void accelerate() { cout << "Increasing speed: "; }
    };
    class Aeroplane : public Vehicle {
    public:
    void accelerate(int height) {
    cout << "Accelerating at a height of: " << height;
    }
    };

int main() {
Aeroplane plane;
plane.accelerate(1000);
plane.Vehicle::accelerate();
cout << endl;
}

  1. 用using
    using A::func; // 解开父类重载函数的默认隐藏。
#include <iostream>
using namespace std;
class Vehicle {
public:void accelerate() { cout << "Increasing speed: "; }
};
class Aeroplane : public Vehicle {public:using Vehicle::accelerate;void accelerate(int height) {cout << "Accelerating at a height of: " << height;}
};int main() {Aeroplane plane;plane.accelerate(1000);plane.accelerate();cout << endl;
}
  1. 当然也可以直接在子类的函数里面调用
class Aeroplane : public Vehicle {public:void accelerate(int height) {Vehicle::accelerate();  cout << "Accelerating at a height of: " << height;}
};

关于overload

关于override
如果不用virtual,怎么实现多态呢?可以用cast。但是对不同子类的object都要cast到对应的子类类型,显然很不方便。
static_cast<Circle *>(s)->draw();

#include <iostream>
#include <vector>using namespace std;class Shape {
public:void draw() { cout << "Drawing a generic shape...\n"; }
};class Circle: public Shape {
public:void draw() { cout << "Drawing a circle...\n"; }
};int main() {vector<Shape *> shapes;      // Vector of pointers to Shape instances// Create a pointer to a child class of Shape and append it to the vector shapes.push_back(new Circle);for (auto s: shapes)static_cast<Circle *>(s)->draw();      for (auto s : shapes)       // Release allocated memorydelete s;
}
http://www.lryc.cn/news/165635.html

相关文章:

  • 算法通关村18关 | 透析回溯的模板
  • 【论文阅读】Untargeted Backdoor Attack Against Object Detection(针对目标检测的无目标后门攻击)
  • 分库分表---理论
  • [golang 流媒体在线直播系统] 2.搭建基于golang的流媒体服务器实现拉流推流,以及Html客户端拉取hls类型的流
  • 9月12日作业
  • React框架下如何集成H.265网页流媒体视频播放器EasyPlayer.js?
  • 《向量数据库》——向量数据库的使用场景有哪些?
  • Java 中 List 集合取补集
  • 我的个人网站——宏夏Coding上线啦
  • 【机器视觉】喇叭的外圆以及金属内圆的同心度视觉检测--康耐德智能
  • STM32WB55开发(2)----修改蓝牙地址
  • 【1++的C++进阶】之C++11(二)
  • 【VS2022】调试
  • python:使用RESTful API(flask)调用python程序传递参数,实现Web端调用python程序
  • 贪心算法(Greedy Algorithm)
  • 论文阅读 - Outlier detection in social networks leveraging community structure
  • 【操作系统】进程控制
  • Linux命令200例:expr一个用于进行数值表达式求值的工具
  • 当你的公司突然开始大量的裁员,被留下的你,真的准备好面对以后了吗?
  • 预约陪诊就诊小程序源码多城市开发版
  • upload-labs文件上传靶场实操
  • leetcode分类刷题:队列(Queue)(二、优先队列解决TopK简单问题)
  • 【排障记录】扩展坞USB 3.0能用而2.0不能用
  • 01-从JDK源码级别剖析JVM类加载机制
  • AI时代:探索机器学习与深度学习的融合之旅
  • 模块化开发_groupby查询think PHP5.1
  • elementUI时间选择器
  • 第1章_瑞萨MCU零基础入门系列教程之单片机程序的设计模式
  • 【UE】刀光粒子效果——part2 材质函数部分
  • 为什么项目经理的时间观念这么重?