Should I use virtual 'Initialize()' functions to initialize an object of my class?

后端 未结 13 1528
孤独总比滥情好
孤独总比滥情好 2020-12-17 14:33

I\'m currently having a discussion with my teacher about class design and we came to the point of Initialize() functions, which he heavily promotes. Example:

13条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 14:52

    Not calling Initialize may be easy to do accidentally and won't give you a properly constructed object. It also doesn't follow the RAII principle since there are separate steps in constructing/destructing the object: What happens if Initialize fails (how do you deal with the invalid object)?

    By forcing default initialization you may end up doing more work than doing initialization in the constructor proper.

提交回复
热议问题