I was going through the, "Multiple Inheritance for C++ by Bjarne Stroustrup, Published in the May 1999 issue of "The C/C++ Users Journal"". The below excerpt is from the same (Page 5/17),
4.4 Casting
Explicit and implicit casting may also involve modifying a pointer value with a delta:
class A { void f(); }; class B { int f(); }; class C : A, B { }; C* pc; B* pb; pb = (B*)pc; // pb = (B*)((char*)pc+delta(B)) pb = pc; // pb = (B*)((char*)pc+delta(B)) pc = pb; // error: cast needed <-------------------- HERE pc = (C*)pb; // pc = (C*)((char*)pb-delta(B))
He shows us that pb = pc
can be done without casting it explicitly. That definitely means that the casting is handled implicitly. Then,
- why, when we try to
pc = pb
pointer, is casting necessary? - What and where is this rule that directs this?
- Is it related to the incrementing/decrementing the pointer by delta value?
EDIT
Jonathan Mee marked this question as the duplicate of "What Type of Cast to Go from Parent to Child?". I am afraid, I don't concur. My question is regarding, why casting and where is this rule that directs us to cast or not to cast. I think the logic behind might be same but the concept is entirely different. In his question, he is doubtful on (insists on not using dynamic cast) the use of dynamic_cast and static_cast. My doubt is still some steps behind his.