Avoid calling constructor of member variable

后端 未结 9 1552
礼貌的吻别
礼貌的吻别 2021-02-19 13:14

I have the following C++-class:

// Header-File
class A
{
    public:
    A();

    private:
    B m_B;
    C m_C;
};

// cpp-File
A::A()
: m_B(1)
{
    m_B.doSom         


        
9条回答
  •  不知归路
    2021-02-19 14:19

    What you ask is forbidden - and correctly so. This ensures that every member is correctly initialized. Do not try to work around it - try to structure your classes that they work with it.

    Idea:

    • C has a constructor that does nothing
    • C has an initialization method that makes the class usable
    • C tracks whether it has been initialized correctly or not and returns appropriate errors if used without initialization.

提交回复
热议问题