How does compiler resolves virtual functions accessing non-virtual data
members C++
I was reading about the C++ Objects when I got this doubt. Suppose there
are two classes
class X
{
virtual int def() { };
}
class Y
{
virtual int abc() { };
}
class Z : public X,public Y
{
virutal int abc() { return a };
int a;
}
Now what I understand is both Y and Z have the reference to Z::abc() in
their virtual tables which helps to resolve the correct function to call.
Consider
Y *y = new Z;
Z *z = new Z;
y->abc() // I understand this is done by reaching the vptr of y by this =
this + sizeof (X)
and z->abc() // z has its own vptr reachable
My understanding is that in both the cases the "this" pointer is passed
and after finding out the correct abc() to call, how does the programm
reach the int value "a"?
How does the compiler calculate the address of " int a" correctly based on
type of the object passed?
No comments:
Post a Comment