一.类中函数加const重载
例子1:
class VAC
{
public:
int f() const { return 1; }
int f() { return 2; }//重载上面函数
};
int main()
{
VAC v1;
const VAC v2;
cout << v1.f() << v2.f();
return 0;
}
函数int f() const 中const修饰this指针。const VAC v2,const也修饰this指针,因此匹配int f() const函数。
例子2:
class VAC
{
public:
int f(int a) { return 3; }
int f(const int a) { return 4; }
};
int main()
{
VAC v1;
int a = 1;
const int b = 2;
cout << v1.f(a) << v1.f(b);
return 0;
}
在函数参数中+const,编译器不认为是函数重载。