问题描述
我有一个对象数组
Passenger travellers[] = {
Passenger(nullptr, "Toronto", 2018, 4, 20),
Passenger("", "Toronto", 2018, 4, 20),
Passenger("John Smith", nullptr, 2018, 4, 20),
Passenger("John Smith", "", 2018, 4, 20),
Passenger("John Smith", "Toronto", 2018, 4, 20), // valid
Passenger("John Smith", "Toronto", 2028, 4, 20),
Passenger("John Smith", "Toronto", 2014, 4, 20),
Passenger("John Smith", "Toronto", 2020, 12, 31), // valid
Passenger("John Smith", "Toronto", 2018, 40, 20),
Passenger("John Smith", "Toronto", 2018, 0, 20),
Passenger("John Smith", "Toronto", 2017, 1, 1), // valid
Passenger("John Smith", "Toronto", 2018, 4, 0),
Passenger("John Smith", "Toronto", 2018, 4, 32),
Passenger(nullptr, nullptr, 0, 0, 0),
Passenger()
};
和我的构造函数是:
默认构造函数
Passenger::Passenger() {
p_name[0] = '\0';
p_dest[0] = '\0';
// destination date
d_yy = 0;
d_mm = 0;
d_dd = 0;
}
我的其他带有参数的构造函数是:
And my other constructor with parameters are:
Passenger::Passenger(const char *name, const char *destination, int year, int month, int days) {
if (name != nullptr && destination != nullptr && name[0] != '\0' && destination[0] != '\0') {
if (year >= 2017 && year <= 2020 && month >= 1 && month <= 12 && days >= 1 && days <= 31) {
strncpy(p_name, name, 32);
strncpy(p_dest, destination, 32);
d_yy = year;
d_mm = month;
d_dd = days;
}
else
Passenger();
}
else
Passenger();
}
问题是,其余对象应该返回无效",但第一个对象却返回有效",即使它具有 nullptr 值.有人知道我在做什么错吗?
Problem is the rest of the objects should return "not valid" but the very first object is returning "valid" instead even though it has a nullptr value. Anyone know what I'm doing wrong?
推荐答案
您不能从其他实体的内部调用默认的
构造函数,就像您尝试做的那样. Passenger()
构造函数> Passenger(...)
You cannot call the default Passenger()
constructor from inside of the BODY of the other Passenger(...)
constructor like you are trying to do.
您实际上是在构造临时的 Passenger
对象,这些对象会立即超出范围:
You are actually constructing temporary Passenger
objects that go out of scope right away:
Passenger::Passenger(const char *name, const char *destination, int year, int month, int days)
{
if (name != nullptr && destination != nullptr && name[0] != '\0' && destination[0] != '\0')
{
if (year >= 2017 && year <= 2020 && month >= 1 && month <= 12 && days >= 1 && days <= 31)
{
strncpy(p_name, name, 32);
strncpy(p_dest, destination, 32);
d_yy = year;
d_mm = month;
d_dd = days;
}
else {
Passenger(); // <-- TEMP OBJECT!!
}
}
else {
Passenger(); // <-- TEMP OBJECT!!
}
}
您不会像预期的那样默认构造使用输入值构造的当前对象.
You are NOT default constructing the current object that is being constructed with input values, like you are expecting.
您可以调用同一类的另一个构造函数的唯一位置是在成员初始化列表中(这是C ++ 11功能,称为委派构造函数),例如:
The ONLY place that you can call another constructor of the same class is in the member initialization list (this is a C++11 feature known as Delegating Constructors), eg:
Passenger::Passenger(const char *name, const char *destination, int year, int month, int days)
: Passenger() // <-- setup default values here...
{
// overwrite default values as needed...
if ((name) && (destination) &&
(name[0] != '\0') && (destination[0] != '\0') &&
(year >= 2017) && year <= 2020) &&
(month >= 1) && (month <= 12) &&
(days >= 1) && (days <= 31))
{
strncpy(p_name, name, 32);
strncpy(p_dest, destination, 32);
d_yy = year;
d_mm = month;
d_dd = days;
}
}
这篇关于C ++,如果条件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!