今天学继承的时候发现一个问题,问了下群内大佬解惑,具体问题如下:
//base class
#include<string>
using std::string ;
// simple base class class
TableTennisPlayer{
private:
string firstname ;
string lastname ;
bool hasTable ;
public :
TableTennisPlayer ( const string & fn =" none ", const string & ln =" none ", bool ht = false );
void Name () const ;
bool HasTable () const { return hasTable ;};
void ResetTable ( bool v ){ hasTable = V ;};
};
class RatedPlayer{
private:
unsigned int rating ;
public :
RatedPlayer ( unsigned int r =0, const string & fn ="none",const string & ln ="none",
bool ht = false );
RatedPlayer ( unsigned int r , const TableTennisPlayer & tp );
unsigned int Rating () const ( return rating ;}
void ResetRating ( unsigned int r ) rating = r ;}
};
问题:
这个派生出来的类如果是这样的话
RatedPlayer a(3,"xx");
不会有二义性嘛
xx可以通过string的构造函数转换成string对象,然后再通过基类的构造函数转换成基类,之后再通过构造函数给a赋值;
或者xx转换成string类之后直接通过构造函数给a赋值;
答案:
调用第二个构造器需要经过两次user defined conversion(const char* -> std::string -> TableTennishPlayer),因此不会被考虑,因为标准规定最多只会考虑一次user defined conversion
为了避免社区内太多垃圾文章,此帖将在本人学完C++基础知识后整合进常见问题内。