What (not) to do in a constructor

后端 未结 13 1702
悲&欢浪女
悲&欢浪女 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:55

    A constructor is used to construct an object - nothing more and nothing less. You need to do whatever it takes to establish the class invariants within a constructor and how complex that is it really depends on the type of the object being initialized.

    Separate init() functions are a good idea only if for some reason you can't use exceptions.

提交回复
热议问题