c++ - Is there any diffrence between {} or default in initializing constructor -
is there difference (no matter how tiny is) between 3 methods of defaulting constructor of class:
directly in header using {}:
//foo.h class foo{ public: foo(){} } directly in header using default keyword:
//foo.h class foo{ public: foo()=default; } in cpp using {}
//foo.h class foo{ public: foo(); } //foo.cpp #include "foo.h" foo::foo(){}
yes, there difference.
option 1 , 3 user-provided. user-provided constructor non-trivial, making class non-trivial. has few effects on how class can handled. no longer trivially copyable, cannot copied using memcpy , like. not aggregate, cannot initialized using aggregate-initialization
a fourth option following:
//foo.h class foo{ public: foo(); } //foo.cpp #include "foo.h" foo::foo()=default; although may seem analogous second example, user-provided well.
functionally, defaulted constructor same thing foo(){}, specified in [class.ctor]/6.
Comments
Post a Comment