class A
{
}
class B : public A
{
}
class C : public B
{
}
Inheritance and base class pointers
Just before I go and re-write half my code when it might not even be necessary:
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?
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
Popular Topics
Advertisement