C++函数重载

来源:互联网 发布:淘宝店铺的图片 编辑:程序博客网 时间:2024/06/06 04:28

在C++中编译器区分不同重载函数的依据是函数调用时的实参,而不是返回值,或者参数+返回值


如果两个函数只有返回值不同,编译器是不认为其为函数重载的


#include <iostream>using namespace std;class A{public:    void f()    {cout<<"void return fuction"<<endl;}    int f()    {cout<<"int return fuction"<<endl;}}int main(){   A a;   a.f();}

上述代码编译时,会报

error:functions that differ only in their return type cannot be overloaded

 

0 0
原创粉丝点击