Function Calls, Part 4 (What Exactly Is “this”?)

Introduction and Series Recap

  • In Part 1, you looked at the basic structure of code generated surrounding a function call.
  • In Part 2, you looked at calling conventions and studied the code generated for two popular calling conventions, __stdcall and __cdecl.
  • In Part 3, you looked at stack frame and studied the positioning of local variables and function arguments in the frame.

In this article, you will explore a slightly more complex data type, class. You may wonder what any of this has got to do with functions. Suffice it to say that, for now, classes can have member functions, which after all are functions. And, because you are on the topic of functions, take a look at them, a little closer look.

Groundwork

I will not go into what classes are and things like that. I assume readers would be familiar with this already. In this part, you will concern yourself with the member functions of a class.

Member functions of a class are similar to the non-member functions, in that they have a return type associated with them; they could take arguments and they could have local variables. Where member functions will differ from their other counterpart—in other words, the non member function types—is in the fact that, apart from the local variables, the member functions have access to the object’s data.

Here’s an example:

int result1 = funcA(1,2);
classA objA;
int result2 = objA.memberFuncA(1,2)

Looking at the code above, you see the striking similarity in how the function is called in the two cases, member function and non-member function. In fact, that is the case. Member function calls are in fact normal function calls. So, everything I talked about in the earlier parts about argument passing via stack, return value passing, pushing of return address on the stack, all work exactly the same way.

In case of a member function call, objA. indicates that the memberFuncA could access objA object’s data. How does this work? How does memberFuncA code know what the objA’s data is? It is not passed in as an argument, as you can see. Does it mean that the compiler is doing something silently and passing in all the objA’s data as arguments, or perhaps, the compiler is passing in the objA’s address as an argument to memberFuncA… or is it something else?

Digging into Disassembly

You have the background from the earlier parts on how to analyze this. Start digging in now.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read