Typically a cast of a pointer to another type does not change the value. However, in some cases, C++ changes the pointer to the instance of the class to support multiple inheritance. Most C++ programmers are unaware of this side effect of multiple inheritance. Suppose we have a C++ class
CA:
class CA : public IX, public IY { ... }
Since
CA inherits
IX and
IY, we can use a pointer to the
CA wherever you can use a pointer to the
IX or
IY. A pointer to the
CA can be passed into a function that takes a pointer to the
IX or
IY, and the function will work correctly. For example:
void foo(IX* pIX);
void bar(IY* pIY);
int main() {
CA* pA = new CA;
foo(pA);
bar(pA);
delete pA;
return 0;
}
foo requires a pointer to a pointer to the virtual functions table of class
IX, and the bar requires a pointer to a pointer to the virtual functions table of class
IY. The contents of virtual functions tables of classes
IX and
IY, of course, is different. We can not transfer
vtbl pointer of class
IX into the
bar function and expect that the function works. Thus, the compiler can not pass the same pointer into the
foo and bar functions; it should modify the pointer to the
CA class so that it points to the appropriate pointer of virtual functions table.
Login in to like
Login in to comment