When/why to make function private in class?

后端 未结 7 756
攒了一身酷
攒了一身酷 2020-12-28 15:50

When should i make a function private and why is it good idea?

7条回答
  •  被撕碎了的回忆
    2020-12-28 16:16

    private: only used by this class, not used by other classes nor derived classes.
    protected: used by this class and maybe derived classes, but not used by other classes.
    public: used by other class, this class, and derived class.

    It's hard to choose between private and protected. So I always make a function protected if there is 1% chance that derived classes may need it.

提交回复
热议问题