Upcoming Posts

Upcoming Posts....

Make your own partition magic software.
How to make an assembler.
What is booting?
Write a simple OS!

‎"I have no special talents. I am only passionately curious." - Albert Einstein

Wednesday, February 15, 2012

How is 'this' pointer passed to the member method?

We know the 'this' pointer points to the object being worked on. All the member methods access object's data using 'this' pointer. C++ Standards doesn't say anything about how to pass 'this' pointer to the Member function. It depends on compiler's calling convention implementation. There are two ways to pass 'this' pointer to the member methods:

1. Pass 'this' pointer to method as argument by pushing it on stack. GCC/CC uses this mechanism.

For example, you can consider that compiler will teat 'void test::display()' method as 'void test::display(test *this)'.

2. Copy 'this' pointer on a register (ECX register is used by VC++). Microsoft VC++ uses this mechanism to provide 'this' pointer to the member methods.

test a;
a.compile_time_binding_method();
     lea ecx,[a] // Address of object 'a' is getting copied in ECX register
     call test::compile_time_binding_method (41118Bh)

You can refer 'Calling Convention' for more information.

No comments:

Post a Comment