How to access protected members in a derived class?

前端 未结 6 2070
说谎
说谎 2021-01-14 01:47

From http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5

A member (either data member or member function) declared in a protecte

6条回答
  •  我在风中等你
    2021-01-14 01:54

    You are not accessing protected function in your derived class, you are trying to overload it and promote from protected to public. This is a forbidden action, you only can hide functions in derived class, e.g. overload protected function as a private.

    Accessing protected function means call it from some member of a class:

    class Y : public X
    {
    public:
        void call() {
          fun();
        }
    }
    

    or like you call it by objX.fun(); is also correct.

提交回复
热议问题