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

后端 未结 13 1500
孤独总比滥情好
孤独总比滥情好 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 15:06

    I"m against 'double initialization' in C++ whatsoever.

    Arguments against constructors:

    • can't be overridden by derived classes
    • can't call virtual functions

    If you have to write such code, it means your design is wrong (e.g. MFC). Design your base class so all the necessary information that can be overridden is passed through the parameters of its constructor, so the derived class can override it like this:

    Derived::Derived() : Base(GetSomeParameter()) 
    {
    }
    

提交回复
热议问题