Class construction with initial values

后端 未结 8 1899
别那么骄傲
别那么骄傲 2020-12-06 20:05

I\'m new to C++, and the whole idea of classes - I\'m still reading a book to try and learn. The book I\'m reading says that when I construct a class, I can assign default v

8条回答
  •  悲&欢浪女
    2020-12-06 20:23

    If the members had non-trivial constructors, in the code below first the default constructors would be called, then the assignments would be executed, while in the code above they would be initialized only one time. So yes, there may be a performance issue.

    There is also a practical issue: if they are const, references, or don't have default constructors, you can't use the version below.

提交回复
热议问题