C++: Initialization of inherited field

前端 未结 4 1180
长发绾君心
长发绾君心 2020-12-02 01:36

I\'ve a question about initialization of inherited members in constructor of derived class. Example code:

class A
    {
public:
    int m_int;
    };

class          


        
相关标签:
4条回答
  • 2020-12-02 02:01

    In order to construct an instance of class B you first instantiate an instance of class A. During that instantiation m_int gets initialized. It's after that intialization that b's constructor is called, so you can't reinitialize m_int. If that's your goal then you can implement a constructor for A that takes an int and then call that in B's initialization list:

    class A
    {
    public:
      A(int x): m_int(x) {}
      int m_int;
    };
    
    class B: public A
    {
    public:
      B(): A(2) {}
    };
    
    0 讨论(0)
  • 2020-12-02 02:16

    You need to make a constructor for A (it can be protected so only B can call it) which initializes m_int just as you have, then you invoke :A(0) where you have :m_int(0)

    You could also just set m_int = 0 in the body of B's constructor. It is accessible (as you describe) it's just not available in the special constructor syntax.

    0 讨论(0)
  • 2020-12-02 02:19

    make a constructor in A and use B(): A(2) {} insteed of B():m_int(0){} its working.

    0 讨论(0)
  • 2020-12-02 02:27

    What you want is this:

    class A{
    public:
        A() : m_int(0);
        int m_int;
    };
    

    so that m_int is initialized in the correct place.

    Edit:

    From a comment above, the reason the compiler complains when you try to initialize the m_int variable in B is that it's already been initialized by the constructor of A. That is, you can't re-initialize something, only reassign. So, you can reassign like Ben Jackson stated above or you can initialize in the proper place.

    0 讨论(0)
提交回复
热议问题