C++ Constructor initialization list strangeness

前端 未结 5 1484
离开以前
离开以前 2021-02-20 05:58

I have always been a good boy when writing my classes, prefixing all member variables with m_:

class Test {
    int m_int1;
    int m_int2;
public:
    Test(int          


        
5条回答
  •  逝去的感伤
    2021-02-20 06:23

    Yes, it's valid. The names in the member initializer list are looked up in the context of the constructor's class so int1 finds the name of member variable.

    The initializer expression is looked up in the context of the constructor itself so int1 finds the parameter which masks the member variables.

提交回复
热议问题