识别构造函数
构造
必要条件
1.本作用域第一次调用的成员函数
2.调用约定必须是__thiscall
- 第一个参数为this指针(离call最近的一个)
- 默认使用ecx传参
3.构造的返回值为this指针
代码示例
#include <string.h>
#include <stdio.h>
class CTest
{
int m_nID;
float m_fltHeight;
public:
CTest()
:m_fltHeight(0.f)
{
m_nID = 0;
}
~CTest()
{
printf("~tagTest()\r\n");
}
};
int main(int argc, char* argv[])
{
CTest test;
return 0;
}
构造函数外
构造函数内
析构
必要条件
1.本作用域最后一次调用的成员函数
2.调用约定必须是__thiscall
- 第一个参数为this指针(离call最近的一个)
- 默认使用ecx传参
3.析构无返回值
代码示例
#include <string.h>
#include <stdio.h>
class CTest
{
int m_nID;
float m_fltHeight;
public:
CTest()
:m_fltHeight(0.f)
{
m_nID = 0;
}
~CTest()
{
printf("~tagTest()\r\n");
}
};
int main(int argc, char* argv[])
{
CTest test;
return 0;
}
析构函数外
析构函数外
识别成员函数
特征
1.参数有this指针
- 默认使用ecx传参
- 第一个参数为this指针
2.函数内有针对第一个参数this指针作间接访问
3.区别调用约定
- __cdecl:传参顺序从右往左;使用栈顶传递参数;外部平栈
- __stdcall:传参顺序从右往左;使用栈顶传递参数;内部平栈
- __fastcall传参顺序从右往左;左边前两个基本属性类型的参数通过寄存器传递,其余各参数使用栈 顶传递参数;内部平栈
- __thiscall:传参顺序从右往左;使用栈顶传递参数,__thiscall只能够用在类的成员函数上,this指针通过ecx传递(该寄存器由编译器决定。VC使用ecx,Borland的C++编译器使用eax);对参数个数确定的内部平栈,否则外部平栈
代码示例
#include <string.h>
#include <stdio.h>
class CTest
{
int m_nID;
float m_fltHeight;
public:
void __fastcall SetID(int nNewID)
{
m_nID = nNewID;
}
int __stdcall GetID()
{
return m_nID;
}
CTest():m_fltHeight(0.f)
{
m_nID = 0;
}
~CTest()
{
printf("~tagTest()\r\n");
}
void SetHeight(float fltHeight)
{
m_fltHeight = fltHeight;
}
float __cdecl GetHeight()
{
return m_fltHeight;
}
};
int main(int argc, char* argv[])
{
CTest test;
test.SetID(7);//__fastcall
test.SetHeight(700.0f);//__thiscall
printf("%d\r\n", test.GetID());//__stdcall
printf("%f\r\n", test.GetHeight());//__cdecl
return 0;
}