C++: overriding public\private inheritance

后端 未结 5 796
慢半拍i
慢半拍i 2020-11-30 08:41

If B inherits from A using public, can B override one of the functions and force it to be private?

class          


        
5条回答
  •  一个人的身影
    2020-11-30 09:36

    ==> If B inherits from A using public, can B override one of the functions and force it to be private?

    NO. Pointer/reference to A will always see my_func2 as public. You can still call this method using A* or A&. (what you ask is possible in Java).

    ==> if the inheritance type is private - can B force a specific function to be public?

    At 1st place if the inheritance type is private/protected then you can NOT assign object of Derived class to Base class pointer/reference. e.g. you can not do following!!

    A* p = new B; // error
    

    ==> What if A is pure abstract? does it make a difference?

    NO difference (except you have to define methods in B)

    ==> Would protected make any difference in any combination?

    NO difference (with respect to Base class)

提交回复
热议问题