1. c++中类的构造函数不会自动初始化类的变量,它们的值将取决于内存中读取的值,所以,在构造函数中,建议对成员变量赋上初值.
2. 当为一个类设定了有参数的constructor之后,编译器将不会为类提供默认的构造函数, 这时,可以使用下列两方法之一提供默认的构造函数
A::A(){};
//or
A::A() = default;
但是,这两种是有区别的, 变量的trival 和pod属性是不一样的.(trival和pod分别是什么含义呢?)
#include <type_traits>
struct X {
X() = default;
};
struct Y {
Y() { };
};
int main() {
static_assert(std::is_trivial<X>::value, "X should be trivial");
static_assert(std::is_pod<X>::value, "X should be POD");
static_assert(!std::is_trivial<Y>::value, "Y should not be trivial");
static_assert(!std::is_pod<Y>::value, "Y should not be POD");
}