简体   繁体   中英

Initialization of an object of class/struct

When trying initialize an object for MyClass, I saw these two methods are used:

MyClass a;
MyClass a{};

Both cases seem to work fine, but I am wondering is there a better practice that one should use which in certain scenarios?

There is a subtle different between MyClass a; and MyClass a{}; . If Myclass is a fundamental type or an aggregate then

MyClass a;

will leave a 's sate/members uninitialized while

MyClass a{};

will value initialize a 's state/members.

If Myclass is neither of the above then they will have the same behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM