By calling a member function in C ++, internally, the instance is passed as the hidden first argument (note that this behavior is strictly determined by the behavior). The C ++ standard does not have the right to talk about this issue, its just a very common way to implement it):
xf(y); // is treated internally as: f(&x, y);
This first argument is then accessible using the this
pointer.
now Afunc
in the example above internally has the signature void Afunc(A* const this)
, and CFunc
has the internal signature void CFunc(C* const this)
.
Please note that the types of arguments are different in both cases, so when you call functions on the same object, you need to pass a different pointer. C ++ solves this by defining an implicit conversion from any derived object to its base object. That is, in the following code:
C* pc = something; pc->Afunc();
This code is processed internally similar to the following (pseudocode):
C* pc = something; Afunc(static_cast<A*>(pc));
This cast is for single inheritance without an operation (that is, you can simply delete it) with the trick mentioned in the quote: object C
and its parent object A
are stored in the same physical address. An object of type C
, which is stored at address x
in memory, is physically laid out in such a way that its parent object of type A
also stored at address x
, followed by all other members that C
may have (but in your case it has no members, and sizeof(C) == sizeof(A)
).
source share