'this' parameter in virtual function calls
Hi VC++ compiler hackers,
I'd like to know how the compiler passes the 'this' parameter in
virtual function calls. To illustrate:
class Employee
{
public:
Employee(const string& name);
~Employee();
// Accessor
const string& GetName() const;
// Increases the salary of this employee by a given percent.
// Since different kinds of employees have different salaries,
// we leave this to be implemented for each kind of employee.
// (Sorry, I couldn't think of a better real-world example use
// of virtual functions :)
virtual void IncreaseSalary(int percent) = 0;
private:
// ...
};
// An example kind-of employee
class Manager : public Employee
{
public:
Manager(const string& name);
~Manager();
void IncreaseSalary(int percent);
private:
// ...
};
void f(Employee& employee)
{
printf("%s\n", employee.GetName().c_str());
employee.IncreaseSalary(50);
}
My understanding is that, for the 'employee.GetName()' call, the
compiler has mangled Employee::GetName into an ordinary function name
(e.g., Employee__GetName), and the 'employee' object is passed as a
hidden argument as the 'this' paramenter.
But what's happening for the 'employee.IncreaseSalary(50)' call? What
exactly is being passed as the 'this' parameter? The only thing I can
imagine is that the 'employee' object must somehow be converted to an
object of a derived class (such as Manager). But how? The compiler
doesn't even know what's the subclass of this particular Employee,
does it?