类中识别构造和析构和成员函数

识别构造函数

构造

必要条件
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;
}


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值