C++重载函数的使用

来源:互联网 发布:js防水卷材是什么 编辑:程序博客网 时间:2024/05/16 17:24
#include <iostream>using namespace std;class A{public:    A(char* par){cout << "A::A" << endl;}    A(const A& other){cout << "copy" << endl;}    A& operator=(const A& other){cout << "operator =" << endl;}};void test(char* a){     cout << "test==>char*" << endl;}void test(const A& a){     cout << "test==>const A&" << endl;}int main(){    char* str = "gg";    test(str);//调用void test(char* a)    test(A(str));//调用void test(const A& a)    return 0;}

================================

-->若函数void  test(const A& a)定义为const test(A& a), 则test("hh")调用会失败,引用类型初始化错误

(invalid initialization of reference of type ‘A&’ from expression of type ‘char*’)

0 0
原创粉丝点击