Jump to content

  • Log In with Google      Sign In   
  • Create Account


__stdcall Class Constructor


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 greenvertex   Members   -  Reputation: 510

Like
1Likes
Like

Posted 08 October 2012 - 06:49 PM

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

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

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):

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

And Microsoft seems to confirm:

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


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

Edited by greenvertex, 08 October 2012 - 06:51 PM.


Sponsor:

#2 Pointer2APointer   Members   -  Reputation: 283

Like
1Likes
Like

Posted 09 October 2012 - 03:22 PM

_stdcall
pushes parameters backwards (right to left) on the stack.

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

They are simply calling conventions for the compiler when generating object code during compilation.
Yes, this is red text.

#3 greenvertex   Members   -  Reputation: 510

Like
0Likes
Like

Posted 09 October 2012 - 05:06 PM

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?

#4 Pointer2APointer   Members   -  Reputation: 283

Like
0Likes
Like

Posted 10 October 2012 - 02:01 PM

As far as I know, you can't forego any
this
-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
this
pointer as an r-value:

http://en.wikipedia....es_and_r-values

Edited by Pointer2APointer, 10 October 2012 - 02:03 PM.

Yes, this is red text.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS