#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*’)