For an integer we can do it as
class A{
int a;
public:
A(int x = 0){ a = x; }
};
For an integer we can do it as
class A{
int a;
public:
A(int x = 0){ a = x; }
};
很难准确理解你在问什么。我想您是在问如何为std::string
参数指定默认值。如果是这种情况,您可以这样做:
class A{
string a;
public:
A(string x = ""){ a = x; }
};
或者:
class A{
string a;
public:
A(const string &x = string()){ a = x; }
};
但是,在任何一种情况下,您都应该a
使用构造函数的成员初始化列表而不是构造函数的主体来初始化成员:
class A{
string a;
public:
A(string x = "") : a(x) { }
};
class A{
string a;
public:
A(const string &x = string()) : a(x) { }
};