详解C++之函数重载

所属分类: 软件编程 / C 语言 阅读数: 109
收藏 0 赞 0 分享

函数重载本质

c++中通过函数名和函数确定一个函数
所以相同的函数名,不同参数也是可以的
不同于c语言,c语言没有函数重载,函数的本质地址就是函数名
函数重载发生在同一个作用域内

类中的重载

构造函数重载
普通成员函数重载
静态成员函数重载

全局函数、静态成员函数、普通成员函数可以发生重载吗?

本质就是函数名和函数参数不同,并且发生在同一个作用域
静态函数和普通成员函数是可以的
全局函数作用域在全局作用域,所以不可以

问题1:当父类的成员函数和子类的成员函数相等,会发生重载吗?

本质还是上面说的,因为父类和子类的作用域不在同一个

看一段代码

 #include <iostream>

class father{
public:
 father() {
 std::cout << "father()" << std::endl;
 }
 void print() {
 std::cout << "father print" << std::endl;
 }
};

class child : public father{
public:
 child() {
 std::cout << "child()" << std::endl;
 }
 void print(int a) {
 std::cout << "child print = " << a << std::endl;
 }
};

int main(){
 father* father_test = new father();
 father_test->print(); //打印father print

 child* child_test2 = new child();
 child_test2->print(); //编译错误no matching function for call to 'child::print()'
   
 return 0;
}

由打印输出可得第一个打印属于father正常输出,没问题,第二个打印是编译错误,我们其实想访问的是父类的print,但是由于子类定义了print,那么在子类的作用域中,print这个函数现在只存在一个,那就是void print(int a),如果想调用父类的就要显示指定child_test2->father::print();

问题2,子类可以重写父类的函数吗,或者说定义相同的?

看代码

#include <iostream>

class father{
public:
 father() {
 std::cout << "father()" << std::endl;
 }
 void print() {
 std::cout << "father print" << std::endl;
 }
};

class child : public father{
public:
 child() {
 std::cout << "child()" << std::endl;
 }
 void print() {
 std::cout << "child print" << std::endl;
 }
};

int main(){
 father* father_test = new father();
 father_test->print(); //打印father print

 child* child_test2 = new child();
 child_test2->print(); //child print

 return 0;
}

可见是可以定义相同的函数,并重写的

问题3,当全局运算符重载遇上类中的运算符重载,优先级是什么

 #include <iostream>
class child;
class father{
public:
 father() {
 std::cout << "father()" << std::endl;
 }
 bool operator==(const father& e) {
 std::cout << "void print(const father& e)" << std::endl;
 }
};
bool operator==(const father& e1, const father& e2) {
 std::cout << "void print(const child& e1, const child& e2)" << std::endl;
}

class child : public father{
public:
 child() {
 std::cout << "child()" << std::endl;
 }
};


int main(){
 child child1_test;
 child child2_test;

 child1_test==child2_test;
 return 0;
}

输出为void print(const father& e)类中的运算符重载优先级大于全局

当复制兼容遇上全局重载呢?

#include <iostream>
class child;
class father{
public:
 father() {
 std::cout << "father()" << std::endl;
 }
 bool operator==(const father& e) {
 std::cout << "void print(const father& e)" << std::endl;
 }
};
bool operator==(const child& e1, const child& e2) {
 std::cout << "void print(const child& e1, const child& e2)" << std::endl;
}

class child : public father{
public:
 child() {
 std::cout << "child()" << std::endl;
 }
};


int main(){
 child child1_test;
 child child2_test;

 child1_test==child2_test;
 return 0;
}

打印:"void print(const child& e1, const child& e2)"

仅仅变化了,全局的函数参数类型,正常来说类中和全局的==都是可以的,但是当类中的需要一个默认的转换,子类到父类,全局的是直接类型就对的上的,编译器就优先使用全局的

说到运算符重载,我们现在定义函数实现类的功能有三种选择:成员函数,全局函数,全局函数+友元函数

1.看是否需要虚函数,如果需要虚函数,那么肯定是类的成员函数

2.看是否定义的是<<或者>>运算符,我们都知道打印输出是全局<<,但是为什么呢,看一下实际的原型针对String而言ostream& operator<<(ostream& output, const String& string)我们使用的时候就是 std::cout << "haha";
如果定义为类中的就是ostream& operator<<(ostream& output)使用起来就是 "haha"<<"std::cout"看出差别了吧,如果定义在类中使用起来就很别扭,继续上面的结果,如果是<<或者>>运算符,然后如果需要访问private域,那么就把全局函数变为类对应的友元函数

3.当需要对左边的参数进行类型转换,需要定义全局函数,因为类中操作符函数,左边的就是类本身,如果需要访问private域,那么就把全局函数变为类对应的友元函数

4.其他情况为类的成员函数

以上就是详解C++之函数重载的详细内容,更多关于c++之函数重载的资料请关注脚本之家其它相关文章!

更多精彩内容其他人还在看

利用C语言来求最大连续子序列乘积的方法

这篇文章主要介绍了利用C语言来求最大连续子序列乘积的方法,基本的思路以外文中还附有相关ACM题目,需要的朋友可以参考下
收藏 0 赞 0 分享

用C语言判断一个二叉树是否为另一个的子结构

这篇文章主要介绍了用C语言判断一个二叉树是否为另一个的子结构,是数据结构学习当中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现的阶乘,排列和组合实例

这篇文章主要介绍了C语言实现的阶乘,排列和组合的方法,涉及C语言数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言查找数组里数字重复次数的方法

这篇文章主要介绍了C语言查找数组里数字重复次数的方法,涉及C语言针对数组的遍历与判断技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言简单实现计算字符个数的方法

这篇文章主要介绍了C语言简单实现计算字符个数的方法,涉及C语言针对字符串的简单遍历与判定技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

c实现linux下的数据库备份

本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

C++获得文件状态信息的方法

这篇文章主要介绍了C++获得文件状态信息的方法,包括文件状态信息、文件所在磁盘盘符、文件创建时间、访问时间及修改日期等,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言按关键字搜索文件夹中文件的方法

这篇文章主要介绍了C语言按关键字搜索文件夹中文件的方法,涉及C语言文件操作及字符串查找的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言之字符串模糊查询方法的实现

本篇文章主要为大家介绍字符串模糊查询的C语言程序编写方法,有需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现BMP转换JPG的方法

这篇文章主要介绍了C语言实现BMP转换JPG的方法,涉及C#图片格式转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多