在C中OOP! [英] OOP in C!

查看:101
本文介绍了在C中OOP!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我觉得OOP也可以在C中完成。我使用了一个

结构来保存成员变量和函数指针。

结构用作创建新对象的类。但我遇到了一个

的问题。如何从结构中的函数指针指向的函数

中访问这些成员变量。


我真的很感激帮助这个和一个代码示例将是

很棒。


提前谢谢,

Prashanth Ellina

Hi,

I have a feeling that OOP can be done in C also. I have used a
structure to hold member variables and function pointers. The
structure is used as a class to create new ''objects''. But I hit a
problem. How do I access these ''member variables'' from the function
that is pointed to by the function pointer in the structure.

I would really appreciate help with this and a code sample will be
wonderful.

Thanks in advance,
Prashanth Ellina

推荐答案



" Prashanth Ellina" < PR ************* @ hotpop.com>在消息中写道

news:28 ************************** @ posting.google.c om ...

"Prashanth Ellina" <pr*************@hotpop.com> wrote in message
news:28**************************@posting.google.c om...


我觉得OOP也可以在C中完成。


它可以。

我使用了
结构来保存成员变量和函数指针。
结构用作创建新对象的类。但是我遇到了一个
问题。如何从结构中的函数指针指向的函数
访问这些成员变量。


没有自动方式。你需要将

结构(或通常最好的指针)传递给你的

函数。

我真的会感谢这方面的帮助,代码示例将非常精彩。


#include< stdio.h>


struct S

{

int member;

void(* fp)(struct S *);

};


void func(struct S * param)

{

printf("%d \ n",param-> member);

}


int main()

{

struct S object = {42,func};

func( &安培;对象); / *打印42 * /

返回0;

}


-Mike

谢谢提前,
Prashanth Ellina
Hi,

I have a feeling that OOP can be done in C also.
It can.
I have used a
structure to hold member variables and function pointers. The
structure is used as a class to create new ''objects''. But I hit a
problem. How do I access these ''member variables'' from the function
that is pointed to by the function pointer in the structure.
There''s no ''automatic'' way. You''ll need to pass the
structure (or usually best, a pointer to it) to your
function.

I would really appreciate help with this and a code sample will be
wonderful.
#include <stdio.h>

struct S
{
int member;
void (*fp)(struct S *);
};

void func(struct S *param)
{
printf("%d\n", param->member);
}

int main()
{
struct S object = {42, func};
func(&object); /* prints 42 */
return 0;
}

-Mike

Thanks in advance,
Prashanth Ellina



Prashanth Ellina写道:
Prashanth Ellina wrote:
我有一种感觉[Obect面向对象编程]也可以在C中完成。


正确。

我使用了一个结构来保存成员变量和函数指针。


指向*回调*函数的指针。

结构用作创建新对象的类。
但是我点击了一个问题。
如何从结构中的函数指针指向的函数
访问这些''成员变量''。


你必须明确地传递一个指向该函数的指针。

我真的很感谢这个
的帮助,代码示例会非常精彩。
I have a feeling that [Obect Oriented Programming] can be done in C also.
Correct.
I have used a structure to hold member variables and function pointers.
Pointers to *callback* functions.
The structure is used as a class to create new ''objects''.
But I hit a problem.
How do I access these ''member variables'' from the function
that is pointed to by the function pointer in the structure.
You must pass a pointer to the function explicitly.
I would really appreciate help with this
and a code sample will be wonderful.




typedef struct X {

int i;

const

struct X * (* f)(const struct X *);

} X;


const

X * X_actual_f(const X *这个){

fprinf(stdout," this-> i =%d \ n",this-> i);

返回此;

}


内联静态

X X_create(int i){

X值;

value.i = i;

value.f = X_actual_f;

返回值;

}


但是运行时多态的实际实现

不存储指向对象本身函数的指针。

它们存储指向a的指针虚函数表

在类定义中初始化,以便

虚函数与* class *
和* not *任何对象都是该类的实例。


typedef struct X {

int i;

const

void * pV; //虚函数表指针

} X;


const

X * X_actual_f(const X * this){

fprinf(stdout," this-> i =%d \ n",this-> i);

返回此内容;

}


struct vTable_t {

X *(* f)(const X *);

} vTable_t;


vTable_t X_vTable = {X_actual_f; };


X X_create(int i){

X值;

value.i = i;

value.pV =(void *)(& X_vTable);

返回值;

}


const

X * X_virtual_f(const X * this){

return((vTable_t *)(this-> pV)) - > f(this); < br $>
}


您调用X_virtual_f(const X *)而不是X_actual_f(const X *)

以便它可以用于课程


typedef struct Y {

X x;

//。 。 。

} Y;


const

X * Y_actual_f(const Y *);


派生自X

但是调用Y_actual_f(const Y *)而不是X_actual_f(const X *)。


转到Google网上论坛:<

http://groups.google.com/


并搜索


运行时多态性C(长)


in comp。 lang.c新闻组。你应该找到:


来自:E。Robert Tisdale(E.**************@jpl.nasa.gov)

主题:C中的运行时多态(长)

新闻组:comp.lang.c

日期:2004-05-12 22:55:28 PST



typedef struct X {
int i;
const
struct X* (*f)(const struct X*);
} X;

const
X* X_actual_f(const X* this) {
fprinf(stdout, "this->i = %d\n", this->i);
return this;
}

inline static
X X_create(int i) {
X value;
value.i = i;
value.f = X_actual_f;
return value;
}

But actual implementations of run-time polymorphism
don''t store pointers to functions in the object itself.
They store a pointer to a virtual function table
which is initialized in the class definition so that
the virtual functions are associated with the *class*
and *not* any object which is an instance of that class.

typedef struct X {
int i;
const
void* pV; // virtual function table pointer
} X;

const
X* X_actual_f(const X* this) {
fprinf(stdout, "this->i = %d\n", this->i);
return this;
}

struct vTable_t {
X* (*f)(const X*);
} vTable_t;

vTable_t X_vTable = { X_actual_f; };

X X_create(int i) {
X value;
value.i = i;
value.pV = (void*)(&X_vTable);
return value;
}

const
X* X_virtual_f(const X* this) {
return ((vTable_t*)(this->pV))->f(this);
}

You call X_virtual_f(const X*) instead of X_actual_f(const X*)
so that it will work for classes

typedef struct Y {
X x;
// . . .
} Y;

const
X* Y_actual_f(const Y*);

derived from X
but call Y_actual_f(const Y*) instead of X_actual_f(const X*).

Go to Google Groups:

http://groups.google.com/

and search for

run-time polymorphism in C (long)

in the comp.lang.c newsgroup. You should find:

From: E. Robert Tisdale (E.**************@jpl.nasa.gov)
Subject: run-time polymorphism in C (long)
Newsgroups: comp.lang.c
Date: 2004-05-12 22:55:28 PST


2004年6月3日星期四16:34:44 -0700,Prashanth Ellina写道:
On Thu, 03 Jun 2004 16:34:44 -0700, Prashanth Ellina wrote:
我真的很感激帮助这个和一个代码示例将是非常好的。
I would really appreciate help with this and a code sample will be
wonderful.




看看这本在线书籍,我认为它会教你一个真正的

处理ANSI C中的OOP。

http://www.planetpdf.com/codecuts/pdfs/ooc.pdf


你也可以在这里找到一些东西:

http://www.eventhelix.com/RealtimeMa ... mming_in_c.htm
http://www.bolthole.com/OO-C-programming.html


祝你有个美好的一天!


Daniele



Have a look at this online book, I think it will teach you a real
deal about OOP in ANSI C.

http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

you can find something also here:

http://www.eventhelix.com/RealtimeMa...mming_in_c.htm
http://www.bolthole.com/OO-C-programming.html

Have a good day!

Daniele


这篇关于在C中OOP!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆