c++ - What happens when you define an empty default constructor? -
i have searched previous questions, , have not found satisfying answer question:
if define empty default constructor class, example
class my_class{ public: myclass(){} private: int a; int* b; std::vector<int> c; }
my understanding if define object using default constructor, say
my_class my_object;
then my_object.a random value, pointer my_object.b random value, vector c well-behaved, empty vector.
in other words, default constructor of c called while default constructors of , b not. understanding correctly? reason this?
thank you!
a
, b
have non-class types, meaning have no constructors @ all. otherwise, description correct: my_object.a
, my_object.b
have indeterminate values, while my_object.c
constructed.
as why... writing user-defined constructor , not mentioning a
, b
in initializer list (and not using c++11 in-class member initializers) explicitly asked compiler leave these members uninitialized.
note if class did not have user-defined constructor, you'd able control initial values of my_object.a
, my_object.b
outside, specifying initializers @ point of object declaration
my_class my_object1; // garbage in `my_object1.a` , `my_object1.b` my_class my_object2{}; // 0 in `my_object2.a` , null pointer in `my_object2.b`
but when wrote own default constructor, told compiler want "override" initialization behavior , yourself.
Comments
Post a Comment