Calling this->get/this->set methods versus directly accesing member variables in C++

前端 未结 8 633
挽巷
挽巷 2021-01-18 18:21

Suppose I have a class Foo, with a private variable bar_ containing some state for Foo. If necessary, I may write public get/set metho

8条回答
  •  隐瞒了意图╮
    2021-01-18 19:24

    If your compiler does any inlining at all, it will be able to inline get and set methods defined and used within a given class. const has no bearing on this, and the inline directive doesn't make much difference either.

    I generally prefer to use my get() and set() methods even from within the class definition, when I can. You get all the same benefits that you get by using them outside the class definition, and there is no speed penalty.

提交回复
热议问题