C ++:覆盖public \private继承 [英] C++: overriding public\private inheritance

查看:217
本文介绍了C ++:覆盖public \private继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 B 使用 public 继承 A 可以 B 覆盖其中一个函数并强制为私有?

  class A 
{
public:
virtual double my_func1(int i);
virtual double my_func2(int i);
}

class B:public A //注意公共继承
{
public:
virtual double my_func1(int i);
private:
virtual double my_func2(int i);
}

另一种方法呢?如果继承类型是private - 可以 B 强制特定函数为public?



如果 A 是纯抽象的吗?



protected 在任何组合中有什么不同吗?

解决方案

如果B使用public继承A,那么B可以覆盖其中一个函数,



my_func1 / code>声明在 priavte 访问说明符下,它仍然可以通过 A类实际指向 B类的对象



调用 my_func1 code>在运行时被评估,取决于指针指向的对象的类型。在编译时,编译将 my_func1()调用视为对 A :: my_func1()的调用, c $ c> A :: my_func1()是public,编译器不报告错误。只有在运行时才会评估实际的函数调用 B :: my_func1()



通过类B 的对象直接调用 my_func1(),但因为 B :: my_func1 在私人访问说明符下声明,并且您无法从类外访问私下声明的成员。



方式?如果继承类型为private - 可以强制某个特定函数为public?

>

如果您通过Base 类A 的指针调用 my_func1() $ c>,在编译时,它只是作为 A :: my_func1()的调用无效 A :: my_func1()在 A类中声明为私有



是纯抽象?是否有区别?

NO

类是抽象或只是多态。

b

正如前两个问题所解释的,如果你通过指向Base类的指针调用一个虚函数,那么在编译时,该成员函数在Base类中的访问,因为编译器将其视为对Base类成员函数的调用。对该函数的实际调用在运行时处计算,该特征称为运行时多态性动态多态性,它独立于作为编译时构造的Access说明符。




$ b

覆盖基类的成员不会影响访问


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

class A
{
public:
    virtual double my_func1(int i);
    virtual double my_func2(int i);
}

class B : public A // Notice the public inheritance
{
public:
    virtual double my_func1(int i);
private:
    virtual double my_func2(int i);
}

How about the other way around? if the inheritance type is private - can B force a specific function to be public?

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

Would protected make any difference in any combination?

解决方案

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

Eventhough the my_func1() is declared under priavte access specifier it can be still called through a pointer to class A, actually pointing to a object of class B

The call to my_func1() is evaluated at run time depending on the type of objected pointed by the pointer. At compile time the compile sees the my_func1() call as call to A::my_func1() and since A::my_func1() is public the compiler doesn't report only error. It is only at runtime that actual function call B::my_func1() is evaluated.

Ofcourse, You cannot directly call my_func1() through object of class B though because B::my_func1() is declared under Private Access specifier and You cannot access privately declared members from outside the class.

How about the other way around? if the inheritance type is private - can B force a specific function to be public?
NO

If you are calling my_func1() through a pointer of the Base class A, At compile time it is just evaluated as call to A::my_func1() which is Invalid since A::my_func1() is declared private inclass A`

What if A is pure abstract? does it make a difference?
NO
It makes no difference if the base class is Abstract or just polymorphic. Same rules will be applicable.

Would protected make any difference in any combination?
NO
As explained in first 2 Q's if you are calling a virtual function thorough pointer to Base class then at compile time the compiler only checks the access of that member function in Base class because compiler sees it as call to Base class member function. The actual call to the function is evaluated at run time and the feature is called Runtime Polymorphism or Dynamic polymorphism which is independent of the Access specifiers, which as a compile time construct.

So in conclusion,

overriding members of Base Class does not affect access

这篇关于C ++:覆盖public \private继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆