Avoid calling constructor of member variable

后端 未结 9 1550
礼貌的吻别
礼貌的吻别 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:08

    Just use comma expressions:

    A::A()
      : m_B(1)
      , m_c(m_B.doSomething(), m_B.doMore(), m_B.getSomeValue())
    {
    }
    

    Obviously, as others have explained, m_B better be declared before m_C else m_B.doSomething() invokes undefined behavior.

提交回复
热议问题