I'm a little bit confused concerning virtual functions.
Lets suppose you have Base class with virtual function foo(), and that function then overridden in Derived class
class Baseclass
{
public:
virtual void foo()
{
//...
}
};
class Derived: public BaseClass
{
private:
int member_val;
public:
Derived( int init )
: member_val( init )
{}
void foo()
{
member_val++;
}
};
and foo using member value of Derived class, when i write this code
Derived d( 10 );
Base* bPtr = &d;
bPtr->foo();
foo() called for Derived class because _vptr points on "Derived class virtual table", and pointer in "Derived class virtual table" points on foo() from Derived class, but how then it found member_val, cause Base pointer doesn't know about it. What "this" is passed to foo() from Derived class. We call it for Base* (this is Base type) but to find member_val we need Derived* ( this Derived type). So how it works under the hood?