Is there any overhead using this-> for accessing a member?

前端 未结 5 1045
死守一世寂寞
死守一世寂寞 2021-01-11 17:33

When accessing a member of some class, I can use e.g.:

this->myVar = 10 

or I can just write:

myVar = 10
5条回答
  •  清酒与你
    2021-01-11 18:18

    I sometimes uses explicit this-> in setters, since they allow me to use the same name for the parameter names as the instance fields:

    class MyClass 
    {
        int foo;
        void setFoo(int foo) 
        {
            this->foo = foo;
        }
    }
    

    however, in constructors, I usually use initialization list, which does not require an explicit this->

    class MyClass 
    {
        int foo;
        MyClass(int foo) : 
            foo(foo) 
        { 
        }
    }
    

提交回复
热议问题