【原创】C语言面向对象,this call C语言实现class


#include <stdio.h>
#include <stdlib.h>







typedef struct user_t
{
    int t;
    char *userName;
    void (*setUserName)(char* name,int);
    void (*print)();

    
}UserT;









/*重点在这里*/
#define THIS_CALL     \
int ex;\
{_asm mov ecx, ebp}\
{_asm sub eax, ecx}\
{_asm sub eax, 4h}\
{_asm mov ecx, eax}\
{_asm mov eax, ebp}\
{_asm go_sub :}\
{_asm add eax, 4}\
{_asm sub ecx, 4h}\
{_asm jg go_sub}\
{_asm mov ecx, esp}\
{_asm mov esp, eax}\
{_asm pop eax}\
{_asm mov ex, eax}\
{_asm mov esp, ecx}



#define FUNC(Type,f,arg) f##arg {\
THIS_CALL \
Type this;\
this = (Type)ex;\


//GO语言定义方式
#define func(Type,f,arg,rt) rt f##arg {\
THIS_CALL \
Type this;\
this = (Type)ex;\



func (UserT*, setUserNamae, (char *name, int a),void)
    this->userName = name;
    this->userName = name;
}


func (UserT*, print, (), void)
    printf("%s\n", this->userName);
}





#define CALL_S(e)\
size_t addr = e; \
{_asm mov eax, esp}\
{_asm push addr}


#define $$(e) {CALL_S(e);} (0,\
e)\

void initUser(struct  user_t* u) {
    u->setUserName = setUserNamae;
    u->print = print;
}




int main() {

    struct  user_t User;initUser(&User);
    $$(&User)->setUserName("张飞", 2);
    $$(&User)->print();
    while (1){}
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM