use*_*203 1 c++ default-constructor
我有一个看起来像这样的课程:
Person(int pID,
int zipCode,
float ySalary,
const string& fName,
const string& mName,
const string& lName)
Run Code Online (Sandbox Code Playgroud)
当我尝试创建一个默认构造函数时,如下所示:
Person::Person(void){
zipCode = NULL;
pID = NULL;
ySalary = NULL;
fName = "";
mName = "";
lName = "";
}
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,说没有运算符"="匹配const std :: string = const char [1];
您需要使用成员初始值设定项列表来初始化const引用成员变量:
Person::Person(void) :
zipCode(NULL) ,
pID(NULL) ,
ySalary(NULL) ,
fName("") ,
mName("") ,
lName("") {
}
Run Code Online (Sandbox Code Playgroud)
我建议始终使用成员初始化列表语法,首选构造函数体中的赋值.请参见此处:在C++中为构造函数使用初始值设定项有什么优势?