cong
cong

Reputation: 149

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?

Upvotes: 1

Views: 286

Answers (10)

justCurious
justCurious

Reputation: 937

#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.

Upvotes: 0

Kunal Vyas
Kunal Vyas

Reputation: 1579

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. Unless you change protected to public in line #10, so that the protocols are followed, and the code looks like this:

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

Upvotes: 0

Marius Bancila
Marius Bancila

Reputation: 16328

The problem is the protected inheritance. B is not an A. B has an A. See this FAQ for more details.

Upvotes: 1

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247979

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

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*.

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122391

Derive B from public A, not protected A:

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

Upvotes: 0

Patrick
Patrick

Reputation: 23619

Class B derives from class A, but marks it as being protected. This means that only subclasses of B 'see' that B derives from 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.

To solve it, change it to this:

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

Upvotes: 8

amit
amit

Reputation: 178451

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.
you can access B as an A in this situation:

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

Upvotes: 2

Boaz Yaniv
Boaz Yaniv

Reputation: 6424

The problem is that you inherit B from A as protected, and therefore outside code is not allowed to 'know' that B inherits from A. Replace protected with public, and the code should compile.

Upvotes: 0

Hyperboreus
Hyperboreus

Reputation: 115

If you change

class B: protected A

to

class B: public A

does it work?

Upvotes: 0

nathan
nathan

Reputation: 5733

It's because you inherited from A with protected. Change it to public and you will get what you expected.

See the C++ FAQ section 24.5 here.

Upvotes: 1

Related Questions