【原創】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