search

Home  >  Q&A  >  body text

c++对象禁止访问类方法

请问各位有没有什么办法禁止实例化的对象调用类内的某个方法呢

高洛峰高洛峰2819 days ago620

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:38:24

    If you want to prohibit access to a class method, put this method in private and protected

    class Test
    {
    public:
        void func1();
    private:
        void func2();
    }

    Here func1 can be called from the object, func2 can only be called inside the class.

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:38:24

    I don’t understand...private methods cannot be called outside the class.

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:38:24

    Yes, write the method (function) to private: After the following, this method is a private method and can only be called in methods of this class. However, objects instantiated by this class cannot call this method. .

    reply
    0
  • Cancelreply