What (not) to do in a constructor

后端 未结 13 1724
悲&欢浪女
悲&欢浪女 2020-12-12 17:33

I want to ask you for your best practices regarding constructors in C++. I am not quite sure what I should do in a constructor and what not.

Should I only use it for

13条回答
  •  没有蜡笔的小新
    2020-12-12 17:50

    Ideally, you should have no code in your constuctors, ever (aside from attribute assignment). There is one important reason: It prevents the composition of objects and makes them un-extensible.

    Here is my blog post about this: Constructors Must Be Code-Free

提交回复
热议问题