C++:覆盖公共私有继承

时间:2023-04-13
本文介绍了C++:覆盖公共私有继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如果 B 使用 publicA 继承,可以 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);
}

反过来呢?如果继承类型是私有的 - B 可以强制一个特定的函数公开吗?

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

如果 A 是纯抽象的怎么办?有什么不同吗?

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

protected 会在任何组合中产生任何不同吗?

Would protected make any difference in any combination?

推荐答案

如果 B 使用 public 从 A 继承,B 是否可以覆盖其中一个函数并强制它是私有的?

即使 my_func1() 是在 privte 访问说明符下声明的,它仍然可以通过指向 class A 的指针调用,实际上指向B 类

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

my_func1() 的调用在运行时根据指针指向的对象类型进行评估.在编译时,编译器将 my_func1() 调用视为对 A::my_func1() 的调用,并且由于 A::my_func1() 是public 编译器不只报告错误.仅在运行时评估实际的函数调用 B::my_func1().

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.

当然,你不能通过class B的对象直接调用my_func1(),因为B::my_func1()是在Private下声明的访问说明符和您不能从类外部访问私有声明的成员.

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.

反过来呢?如果继承类型是私有的 - B 可以强制特定的函数是公共的吗?
没有

如果您通过 Base class A 的指针调用 my_func1(),在编译时它只是被评估为对 A::my_func1 的调用()Invalid 因为 A::my_func1() 在 class A`

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`

如果 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.

保护在任何组合中都有什么不同吗?

如前 2 个问题中所述,如果您正在调用指向基类的虚函数彻底指针,那么在编译时编译器仅检查基类中该成员函数的访问,因为编译器将其视为对基类成员函数的调用.对函数的实际调用在 runtime 进行评估,该功能称为 Runtime PolymorphismDynamic polymorphism,它独立于访问说明符,作为编译时构造.

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.

总之,

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

这篇关于C++:覆盖公共私有继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!