开发者

C++ constructor behavior

开发者 https://www.devze.com 2022-12-27 21:30 出处:网络
I\'m declaring an instance o开发者_JAVA百科f a class like so: Matrix m; This appears to implicitly initialize m (i.e. run the constructor).Is this actually the case?Yes, the default constructor is

I'm declaring an instance o开发者_JAVA百科f a class like so:

Matrix m;

This appears to implicitly initialize m (i.e. run the constructor). Is this actually the case?


Yes, the default constructor is called.

If there is no default constructor, this statement is ill-formed. If there are no user-declared constructors, the compiler provides a default constructor.


Yes, it creates an instance of class Matrix on the stack. This instance is intialized using the default constructor of class Matrix. This instance created on stack will be destroyed when the variable m goes out of scope. When the object is destroyed its destructor will be called.


Yes, syntactically this is equal to writing:

Matrix m();

Although, if there is no default constructor defined the compiler will give an error.

NOTE: If no constructors are defined for a class a default constructor is made by the compiler, but if a constructor with parameters is defined the default constructor is NOT made by the compiler.

0

精彩评论

暂无评论...
验证码 换一张
取 消