The fragment below is from a VC++ 2008 Express Edition. Say, I have a class with a member that is a struct. I am trying to define default values for the member variables of this class. Why this does not work?
以下片段来自VC ++ 2008 Express Edition。说,我有一个类,其成员是一个结构。我正在尝试为此类的成员变量定义默认值。为什么这不起作用?
struct Country{
unsigned chart id;
unsigned int initials;
std::string name;
};
class world{
private:
Country _country;
unsigned int _population;
public:
world(){};
world():
_country():
id('1'), initials(0), name("Spain") {};
_population(543000) {}
:
:
~world(){};
};
2 个解决方案
#1
There are two ways to initialize the country member data. Like this ...
有两种方法可以初始化国家/地区成员数据。像这样 ...
struct Country{
unsigned char id;
unsigned int initials;
std::string name;
};
class world{
private:
Country _country;
public:
world()
{
_country.id = '1';
_country.initials = 0;
_country.name = "Spain";
}
~world(){};
};
... or, like this ...
......或者,像这样......
struct Country{
unsigned char _id;
unsigned int _initials;
std::string _name;
Country(
unsigned char id,
unsigned int initials,
const std::string& name
)
: _id(id)
, _initials(initials)
, _name(name)
{}
};
class world{
private:
Country _country;
public:
world()
: _country('1', 0, "Spain")
{
}
~world(){};
};
Note that in the second example I find it easier to initialize the Country instance because I defined a constructor as a member of the Country struct.
请注意,在第二个示例中,我发现初始化Country实例更容易,因为我将构造函数定义为Country结构的成员。
Or, perhaps you want to give the Country type a default constructor:
或者,您可能希望为Country类型提供默认构造函数:
struct Country{
unsigned char _id;
unsigned int _initials;
std::string _name;
Country()
: _id('1')
, _initials(0)
, _name("Spain")
{}
};
class world{
private:
Country _country;
public:
world()
{
}
~world(){};
};
#2
The structure is an aggregate type.
结构是聚合类型。
Since it has no constructor you cannot initialise it with normal brackets, you can however use curly braces as you would initialise an array.
由于它没有构造函数,因此无法使用普通括号初始化它,因此您可以像初始化数组一样使用花括号。
#1
There are two ways to initialize the country member data. Like this ...
有两种方法可以初始化国家/地区成员数据。像这样 ...
struct Country{
unsigned char id;
unsigned int initials;
std::string name;
};
class world{
private:
Country _country;
public:
world()
{
_country.id = '1';
_country.initials = 0;
_country.name = "Spain";
}
~world(){};
};
... or, like this ...
......或者,像这样......
struct Country{
unsigned char _id;
unsigned int _initials;
std::string _name;
Country(
unsigned char id,
unsigned int initials,
const std::string& name
)
: _id(id)
, _initials(initials)
, _name(name)
{}
};
class world{
private:
Country _country;
public:
world()
: _country('1', 0, "Spain")
{
}
~world(){};
};
Note that in the second example I find it easier to initialize the Country instance because I defined a constructor as a member of the Country struct.
请注意,在第二个示例中,我发现初始化Country实例更容易,因为我将构造函数定义为Country结构的成员。
Or, perhaps you want to give the Country type a default constructor:
或者,您可能希望为Country类型提供默认构造函数:
struct Country{
unsigned char _id;
unsigned int _initials;
std::string _name;
Country()
: _id('1')
, _initials(0)
, _name("Spain")
{}
};
class world{
private:
Country _country;
public:
world()
{
}
~world(){};
};
#2
The structure is an aggregate type.
结构是聚合类型。
Since it has no constructor you cannot initialise it with normal brackets, you can however use curly braces as you would initialise an array.
由于它没有构造函数,因此无法使用普通括号初始化它,因此您可以像初始化数组一样使用花括号。