您的位置:首页 > 编程语言 > C语言/C++

c++中不能区分重载的情况

2015-07-31 15:07 375 查看
Recordlookup(const Account &acct);
Recordlookup(const Account&); // parameter names are ignored

typedefPhone Telno;
Recordlookup(const Phone&);
Recordlookup(const Telno&); // Telno and Phone are the same type

Recordlookup(const Phone&, const Name&);
//default argument doesn't change the number of parameters
Recordlookup(const Phone&, const Name& = "");

// constis irrelevent for nonreference parameters
Recordlookup(Phone);
Recordlookup(const Phone); // redeclaration
ps:仅当形参是引用或指针时,形参是否为 const才有影响。
Recordlookup(Account&);
Record lookup(const Account&);// new function
const Account a(0);
Account b;
lookup(a); // callslookup(const Account&),没有则报错,即使有lookup(Account&);
lookup(b); // callslookup(Account&)精确匹配,如果没有则调用
lookup(const Account&),
注意:不能基于指针本身是否为 const 来实现函数的重载:
f(int*);
f(int*const); // redeclaration,等同于非引用、非指针变量

ps:const成员函数可以区分重载函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: