详解C++之函数重载

所属分类: 软件编程 / C 语言 阅读数: 82
收藏 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++无法重载点符号、::、sizeof等的原因

这篇文章主要介绍了C++无法重载点符号、::、sizeof等的原因的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

实例讲解C语言编程中的结构体对齐

这篇文章主要介绍了C语言编程中的结构体对齐,值得注意的是一些结构体对齐的例子在不同编译器下结果可能会不同,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言的结构体中成员变量偏移问题

这篇文章主要介绍了C语言的结构体中成员变量偏移问题,以讲解如何编写宏来对成员变量进行修改为主,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言结构体中的函数指针

这篇文章主要介绍了详解C语言结构体中的函数指针,文中对函数指针的基本概念也有讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

C++编程中的函数指针初步解析

这篇文章主要介绍了C++编程中的函数指针初步解析,函数指针在C语言和C++学习中都是非常重要的知识,需要的朋友可以参考下
收藏 0 赞 0 分享

实例解析C++中类的成员函数指针

这篇文章主要介绍了C++中类的成员函数指针,例子中以讨论用函数指针调用类的成员函数为主,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的函数指针基础学习教程

这篇文章主要介绍了C语言中的函数指针基础学习教程,包括函数指针作为参数来传递等重要知识,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解析C语言中函数指针的定义与使用

这篇文章主要介绍了C语言中函数指针的定义与使用,是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言编程中的函数指针以及函数回调

这篇文章主要介绍了C语言编程中的函数指针以及函数回调,函数回调实际上就是让函数指针作函数参数、调用时传入函数地址,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的函数指针学习笔记

这篇文章主要介绍了C语言中的函数指针的一些学习知识点记录,文中作者整理了一些比较interesting的函数指针用法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多