getters and setters style

前端 未结 13 2734
猫巷女王i
猫巷女王i 2021-01-01 12:05

(Leaving aside the question of should you have them at all.)

I have always preferred to just use function overloading to give you the same name for both getter and s

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 12:13

    I prefer to avoid the get and set labels, the information is not needed for the compiler to do it's job for most of these simple properties.

    you can have issues:

    class Stuff {
      void widget( int something ); // 'special' setter
      const Widget& widget( int somethingelse ) const; // getter
    }
    Stuff a; 
    a.widget(1); // compiler won't know which widget you mean, not enough info
    

提交回复
热议问题