Sign in to follow this  
greenvertex

__stdcall Class Constructor

Recommended Posts

greenvertex    510
I've seen forms of this around here and there and I don't get it:

[CODE]
class Test {
public:
__stdcall Test()
{
int x = 0;
++x;
}
};
int main(int argc, char** argv)
{
Test t;
return 0;
}
[/CODE]

Why is the constructor declared to use the stdcall convention as opposed to thiscall? It doesn't actually seem to affect the assembly in debug mode (the following appears to be thiscall convention):

[CODE]
12: Test t;
0108143E lea ecx,[t]
01081441 call Test::Test (010810E1h)
13:
14: return 0;
01081446 xor eax,eax
[/CODE]

And Microsoft seems to confirm:

[quote]When you use the __stdcall keyword on a non-static member function, such as a constructor, the compiler will use the thiscall calling convention."[/quote]

This was only tested in VS2012 so maybe this stdcall->thiscall behavior is implementation specific? Edited by greenvertex

Share this post


Link to post
Share on other sites
[CODE]_stdcall[/CODE] pushes parameters backwards (right to left) on the stack.

[CODE]_thiscall[/CODE] gets pushed on the stack, but in normal order, and the [CODE]this[/CODE] pointer is placed in the ECX register.

They are simply calling conventions for the compiler when generating object code during compilation.

Share this post


Link to post
Share on other sites
greenvertex    510
I understand that, which is why stdcall convention doesn't make a whole lot of sense to me when applied to constructors. I'm simply wondering if there is some implementation out there in which it makes sense (or in which it is even allowed) to forego the this pointer?

Share this post


Link to post
Share on other sites
As far as I know, you can't forego any [CODE]this[/CODE]-type reference or pointer to a class object in memory in programming languages(well, at least not in any practical way).

C++ also implements the [CODE]this[/CODE] pointer as an r-value:

[url="http://en.wikipedia.org/wiki/Value_(computer_science)#Assignment:_l-values_and_r-values"]http://en.wikipedia....es_and_r-values[/url] Edited by Pointer2APointer

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this