简体   繁体   English

指向C ++中派生类的基类指针

[英]the base class pointer to the derived class in c++

#include <iostream>
using namespace std;

class A {
    public:
        A ();
        virtual ~A();
};

class B: protected A {
    public:
        virtual ~B ();
};

int main() {
    A* pb = new B;//A is inaccessable base of B
    return 0;
}

when I run the code above, it tells me A is inaccessable base of B, the pb is a pointer, which pointer to the B, what is the problem? 当我运行上面的代码时,它告诉我A是B不可访问的基数,pb是一个指针,指向B的指针是什么问题?

Class B derives from class A, but marks it as being protected. B类源自A类,但将其标记为受保护。 This means that only subclasses of B 'see' that B derives from A. 这意味着,仅B的子类“看到” B从A派生。

Since the main routine is not a subclass of B, it only sees B, not that B derives from A. Therefore, you can't cast a B-pointer to an A-pointer. 由于主例程不是B的子类,因此它仅看到B,而不看到B从A派生。因此,您不能将B指针强制转换为A指针。

To solve it, change it to this: 要解决它,请将其更改为:

class B: public A {
public:
    virtual ~B ();
};

B is protected inherited from A, thus only subclasses of B 'knows' it's an A. the static main() doesn't 'know' it, because it does not inherit from B. B受保护是从A继承的,因此仅B的子类“知道”它是A。静态main()并不“知道”它,因为它不继承自B。
you can access B as an A in this situation: 在这种情况下,您可以将B作为A进行访问:

class C: B {
    void foo() {
        A* pb = new B;
    }
};

if you need it in the main, you would need to change B to public inherit from A 如果您主要需要它,则需要将B更改为从A的公共继承

It's because you inherited from A with protected . 这是因为您从A继承了protected Change it to public and you will get what you expected. 将其更改为public ,您将获得期望的结果。

See the C++ FAQ section 24.5 here . 请参阅此处的C ++常见问题解答部分24.5。

The problem is the protected inheritance. 问题是受保护的继承。 B is not an A. B has an A. See this FAQ for more details. B不是A。B有A。有关更多详细信息,请参见此常见问题解答

If you change 如果你改变

class B: protected A

to

class B: public A

does it work? 它有效吗?

问题是您从A继承了protected B,因此不允许外部代码“知道” B从A继承。用public替换protected,并且代码应该编译。

Derive B from public A , not protected A : public A派生B ,但不受protected A

class B: public A
{
public:
    virtual ~B ();
};

You're using protected inheritance. 您正在使用受保护的继承。 Protected, when applied to members, means that "derived classes can access, but external callers cannot". 当应用于成员时,受保护意味着“派生类可以访问,而外部调用者不能”。 And when applied to inheritance, it means... exactly the same thing. 当应用于继承时,它意味着……完全一样的东西。

So within the B class, you know that it is derived from A , and can cast to an A* and back 因此,在B类中,您知道它是从A派生A ,并且可以转换为A*并返回

BUt outside of B (for example, in the main function), the inheritance isn't visible, and so you can't convert a B* to A* . BUt B 之外 (例如,在main函数中),继承是不可见的,因此您不能将B*转换为A*

#include <iostream>

using namespace std;

class A {
    public:
        A () {};
        virtual ~A();
};

class B: public A {
    public:
        B() {};
        virtual ~B ();
};

int main() {
    A* pb = new B;
    return 0;
}

This seems to work. 这似乎有效。 In your example you make B ready for future inheritance, protected inherit makes A protected and only B methods can see the A. 在您的示例中,您使B准备将来的继承,受保护的继承使A受保护,只有B方法可以看到A。

The property of Inheritance rules out the possibility of derivation of a protected parent class into a subclass. 继承的属性排除了将受保护的父类派生到子类的可能性。 Therefore, deriving the attributes of class A to class B isn't really going to happen in the code. 因此,将类A的属性派生到类B并不会在代码中真正发生。 Unless you change protected to public in line #10 , so that the protocols are followed, and the code looks like this: 除非您在#10行中将protected更改为public ,否则将遵循协议,并且代码如下所示:

class B: public A
{
public:
    virtual ~B ();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM