Inheritance and base class pointers

Started by
1 comment, last by Sparhawk42 18 years, 8 months ago
Just before I go and re-write half my code when it might not even be necessary:

class A
{

}

class B : public A
{

}

class C : public B
{

}

The base pointer for class C is a pointer to an A object, isn't it? So, am I able to refer to an object of type C with a pointer to a B object. Or if I wanted to do that, would I have to do some casting?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
I'm not entirely sure what you are asking, but a C object pointer will have an implicit conversion to an A pointer or a B pointer.

The following is allowed:

A a;
B b;
C c;

A* ap = &c
ap = &b
B* bp = &c


If all your functions are virtual, you will call all functions from c after
ap = &c

If there are some functions in A, which are not virtual or not defined in A.
You will need a cast:

static_cast<C*>(a)->someFunctionOfC(...);

This topic is closed to new replies.

Advertisement