C++ C+中参数的默认值+;建造师
我是否可以像方法1那样立即指定默认值,或者像方法2那样使用重载构造函数,或者像方法3/4那样使用初始化列表 哪种方法更好/正确?为什么(所有方法似乎都有效) 方法3和方法4之间的区别是什么?我应该指定第一个构造函数声明,然后在类外指定下一个定义,还是可以立即指定定义 方法1:C++ C+中参数的默认值+;建造师,c++,constructor,default-value,initialization-list,C++,Constructor,Default Value,Initialization List,我是否可以像方法1那样立即指定默认值,或者像方法2那样使用重载构造函数,或者像方法3/4那样使用初始化列表 哪种方法更好/正确?为什么(所有方法似乎都有效) 方法3和方法4之间的区别是什么?我应该指定第一个构造函数声明,然后在类外指定下一个定义,还是可以立即指定定义 方法1: #include <iostream> #include <string> using namespace std; const string GLOBAL_VAR = "XXX"; class
#include <iostream>
#include <string>
using namespace std;
const string GLOBAL_VAR = "XXX";
class Object
{
private:
string var;
public:
Object(string inArg = "yyy")
{
this->var = GLOBAL_VAR + inArg + "ZZZ";
}
string show()
{
return this->var;
}
};
int main() {
Object o1, o2("www");
cout << o1.show() << endl;
cout << o2.show() << endl;
system("pause");
}
#include <iostream>
#include <string>
using namespace std;
const string GLOBAL_VAR = "XXX";
class Object
{
private:
string var;
public:
Object()
{
this->var = GLOBAL_VAR + "yyyZZZ";
}
Object(string inArg)
{
this->var = GLOBAL_VAR + inArg + "ZZZ";
}
string show()
{
return this->var;
}
};
int main() {
Object o1, o2("www");
cout << o1.show() << endl;
cout << o2.show() << endl;
system("pause");
}
#include <iostream>
#include <string>
using namespace std;
const string GLOBAL_VAR = "XXX";
class Object
{
private:
string var;
public:
//declaration:
Object();
Object(string);
string show()
{
return this->var;
}
};
//definition:
Object::Object() : var(GLOBAL_VAR + "yyyZZZ") {}
Object::Object(string inArg) : var(GLOBAL_VAR + inArg + "ZZZ"){}
int main() {
Object o1, o2("www");
cout << o1.show() << endl;
cout << o2.show() << endl;
system("pause");
}
#include <iostream>
#include <string>
using namespace std;
const string GLOBAL_VAR = "XXX";
class Object
{
private:
string var;
public:
//declaration and definition in one:
Object() : var(GLOBAL_VAR + "yyyZZZ") {}
Object(string inArg) : var(GLOBAL_VAR + inArg + "ZZZ") {}
string show()
{
return this->var;
}
};
int main() {
Object o1, o2("www");
cout << o1.show() << endl;
cout << o2.show() << endl;
system("pause");
}
#包括
#包括
使用名称空间std;
常量字符串全局_VAR=“XXX”;
类对象
{
私人:
字符串变量;
公众:
对象(字符串inArg=“yyy”)
{
这->var=GLOBAL_var+inArg+ZZZ;
}
字符串显示()
{
返回此->变量;
}
};
int main(){
对象o1、o2(“www”);
CUT我认为这两种方法都是同样有效的。根据每个个体的细节,每种方法都有其优点、优点和缺点。
当需要以几乎相同的方式初始化对象时,无论构造函数参数是否为默认值,使用默认值通常是最佳选择。指定默认值可防止重复大量代码。您只有一个构造函数
另一方面,使用重载构造函数可以根据参数是否给定,以完全不同的方式清晰地构造对象。在这种情况下,强制类使用单个构造函数通常会导致代码中出现大量if
语句
另外,不要忘记第三个选项:委托构造函数。在示例代码中使用该类:
Object() : Object("")
{
}
这种方法也有其固有的优势
一般来说,没有一种方法是最好的,最好考虑每个类的个别需求,选择最适合该类的方法。对于一个类来说最好的方法可能不是另一个类的最好方法。
在C++中,你不需要<代码> ->< /Cord>符号。方法2和方法3之间确实没有区别。方法2和方法4之间唯一的区别是一个使用初始值设定项列表,而另一个不使用。这意味着方法2、3和4是相同的。至于哪一个是“最佳的”,这取决于你的想法,这是主观的,也是意见的问题。根据一个委托构造函数,哪个更好:Object(string inArg=“yyy”):var(inArg){var=GLOBAL_var+var+“ZZZ”}
或Object():Object(“yyy”){}Object(string inArg):var(inArg){var(var=GLOBAL_var+var+“ZZZ”}
?对于这种特定的情况,我认为委托构造函数方法更好,至少对于我使用的编译器是这样。但仅针对这个特定的类。这个结论只适用于这个特定的类,而不适用于其他类。换句话说,它的价值很小;这不应该被解释为委托构造函数structor在其他类中或使用不同的编译器时会更好。了解并理解每个类的用法以及编译器,对于给定的类来说,就这个主题得出正确的结论是必需的。