C++ Constructor initialization list strangeness

前端 未结 5 1487
离开以前
离开以前 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:43

    This is perfectly normal behavior. As AAT rightly pointed out, there is no ambiguity. The variables initialised by the list have to be class members. This is standard and works across all compliant compilers.

    The only thing to remember while using a list like this is that a person who doesn't understand this kind of code may have to maintain it. There is nothing wrong with writing initialisation code like this as long as you know what you are doing.

提交回复
热议问题