C语言模仿类中的THIS指针,成功!!
linux吧
全部回复
仅看楼主
level 7
#include <stdlib.h>
#include <stdio.h>
struct abc
{
int a;
void (*func)();
int b;
};
struct abc *new_aa();
struct abc *aa;
struct abc *bb;
void func()
{
asm("movl $15,(%edx)"); //改变 aa->a = 5; //但是没有成功!!
printf("I咯");
}
struct abc *new_aa()
{
struct abc *this;
this = (struct abc*)malloc(sizeof(struct abc));
this->func = func;
return this;
}
int main(int argc,char *argv[])
{
aa = new_aa();
bb = new_aa();
aa->a = 3;
asm("movl %eax,%edx"); //保存结构体 aa 指针的地址
aa->func();
printf("%d-%d\n", aa->a,bb->a);
return 0;
}
2011年10月26日 05点10分 1
level 9
祝贺。
2011年10月26日 05点10分 2
level 7
#include <stdlib.h>
#include <stdio.h>
struct abc
{
int a;
void (*func)();
int b;
};
struct abc *new_aa();
struct abc *aa;
struct abc *bb;
void func()
{
struct abc *cc;
asm("": "=d"(cc));
cc->a = 15;
printf("I咯");
}
struct abc *new_aa()
{
struct abc *this;
this = (struct abc*)malloc(sizeof(struct abc));
this->func = func;
return this;
}
int main(int argc,char *argv[])
{
aa = new_aa();
bb = new_aa();
aa->a = 3;
asm("movl %eax,%edx"); //保存结构体 aa 指针的地址
aa->func();
printf("%d-%d\n", aa->a,bb->a);
return 0;
}
2011年10月26日 05点10分 3
level 7
#include <stdlib.h>
#include <stdio.h>
struct abc
{
int a;
void (*func)();
int b;
};
struct abc *new_aa();
struct abc *aa;
struct abc *bb;
void func(int x,int y)
{
struct abc *cc;
asm("": "=d"(cc));
cc->a = x;
cc->b = y;
}
struct abc *new_aa()
{
struct abc *this;
this = (struct abc*)malloc(sizeof(struct abc));
this->func = func;
return this;
}
int main(int argc,char *argv[])
{
aa = new_aa();
bb = new_aa();
aa->a = 3;
asm("movl %eax,%edx"); //保存结构体 aa 指针的地址
aa->func(12,13);
printf("%d-%d\n", aa->a,aa->b);
return 0;
}
2011年10月26日 05点10分 4
level 7
谢谢,主要是各位给我意见
希望我不要在 button1->size(button1,30,40);
所以我才去研究 C语言内部!去反汇编!
谢谢各位的 意见和批评!
是你们的 意见和批评,我才努力了!
2011年10月26日 05点10分 5
level 9
效率上完全杯具了。。
2011年10月26日 08点10分 6
level 7
效率上差不多! 反汇编一下!都差不多啊··
c++ 反汇编看看它的类! 实例! 构造!析构!
C 反汇编! new 什么的!都差不多的饿·
2011年10月26日 08点10分 7
level 9
这样相当于在C++中把所有函数都定义为virtual了。
2011年10月26日 10点10分 8
level 7
this(Btn)->width(120);
this(Btn)->text("确定");
2011年10月26日 10点10分 9
level 7
我已经将他们封装好了,就像C++一样!
C语言模拟 this!
this(Btn)->width(120);
this(Btn)->text("确定");
以前是这样的 Btn->width(Btn,120);
Btn->text(Btn,"确定");
每次都要传入一个结构体,非常的不美观,也麻烦!
现在好拉!
this(Btn)->width(120);
this(Btn)->text("确定");
2011年10月26日 10点10分 10
level 9
C++在编译期就已经把非virtual函数链接完成了,比你这样在运行期才通过函数指针调用的方法要高效得多。
用C来模仿C++是杯具,这点很多人已经验证过了。
2011年10月26日 11点10分 11
level 14
MARK: TO_INDEX
2011年10月26日 12点10分 12
level 7
没办法,有时候需要用到面向对象的思想!所以需要去这样做!来完成面向对象的思想!
所以一定要这样啦·呵呵呵呵呵!
这个不过是编译器的问题而已·,没事的·
2011年10月26日 13点10分 13
level 9
面向对象的思想就是个this指针吗?你不觉得这么做太丑了点?GObject费这么大事造出来一套面向对象的东西,也没去模仿那个this指针,因为这根本就是无关紧要的形式的差别。
2011年10月26日 13点10分 14
level 9
lz
,其实你这是蛮力。。。
ABI 。。。 杯具。
先把C 函数调用栈帧结构搞清楚。。。
https://tieba.baidu.com/p/1259147942
2011年10月26日 13点10分 15
level 7
/*
Linux下C语言 面向对象!!
简约软件开发小组 作者:暴风
QQ:356752238
邮箱:[email protected]
2011年10月26日 19:00
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct _BUTTON Button;
//将参数传给 edx,保存地址!!
#define _this(_Gtk_gtk) asm(""::"d"(_Gtk_gtk))
//获取edx中保存的地址, 传给this保存!!
#define This_Init(_Gtk) _Gtk *this; \
asm("":"=d"(this))
Button *this(Button *t); //转换函数
void _width(int w);
struct _BUTTON
{
int button_width;
int button_height;
void (*width)();
};
Button *new_button()
{
Button *this;
this = (Button*)malloc(sizeof(Button));
this->button_width = 0;
this->button_height = 0;
this->width = _width;
return this;
}
int main(int argc, char *argv[])
{
Button *button1;
button1 = new_button(); //实例化和构造
this(button1)->width(30);
printf("button的宽度为:%d\n",button1->button_width);
return 0;
}
//转换函数
Button *this(Button *t)
{
Button *this = t;
_this(this);
return this;
}
void _width(int w)
{
This_Init(Button);
this->button_width = w;
}
2011年10月26日 13点10分 16
level 9
救不过来了,让LZ保重把。。。
2011年10月26日 13点10分 17
level 7
无论你如何说,我都要坚持下去自己的想法!
因为我要写GTK+的封装!需要用到 面向对象思想!又不能用C++,因为我希望原生的语言就是C语言! 因为我想弄一个IDE的GTK+开发环境!原生为C语言!然而又不能将 button1->size(button1,200,300); button1又再继续传进去!主要是为了美观,也为了方便!
2011年10月26日 13点10分 18
level 7
我将 GTK+中的 button 封装成一个类!
然后Button *button1;
Button *button2;
就可以继承无法的命令控件,拥有相同的方法!
this(button1)->size(200,300);
this(button2)->text("确定");
所以必须需要,你说的也好,我知道了!
2011年10月26日 13点10分 19
level 9
那哥就跟你算着看看你能坚持几天,今天算第一天如何?打赌不超过一个月。
2011年10月26日 13点10分 20
1 2 尾页