📄 oop.jxh.html
字号:
<html><!-- Mirrored from c-faq.com/struct/oop.jxh.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:27 GMT --><head><title>OOP in C</title></head><body>From: James C Hu<br>Subject: Re: struct inheritence (OOP C)<br>Date: Thu, 22 Jan 1998 19:48:56 -0600 (CST)<br>Message-ID: <slrn6cfto4.ijc.jxh@lambada.cs.wustl.edu><p>On Thu, 22 Jan 1998 15:23:25 -0800, Wayne O. Cochran wrote:<br>> I am writing some OOP code in C and am trying to decide<br>> on the best way to implement inheritence without abusing<br>> the C standard. Say I have a class POINT (implemented<br>> as in the structure below) and I want another class CIRCLE<br>> to inherit the fields of POINT, but I want to be able to<br>> pass instances of CIRCLE to functions that manipulate<br>> instances of POINT. The code below depends on exactly<br>> how structures lay out their fields -- The ANSI C standard<br>> states that the address offsets of the fields occur<br>> in increasing order they appear in the struct template.<br>> Is this abusing the C standard? I note that the X11 Xt<br>> widget toolkit uses a similar techinique for OOP in C.<br>> BTW, I didn't find this one the FAQ.<br>><br>> typedef struct {<br>> int x,y;<br>> } POINT;<br>><br>> typedef struct {<br>> POINT center;<br>> int radius;<br>> } CIRCLE;<br>><br>> void setLocation(POINT *p, int x, int y) {<br>> p->x = x;<br>> p->y = y;<br>> }<br>><br>> CIRCLE c;<br>> setLocation(&c, 10,15);<p>Except for the fact that a cast is required<p><pre>setLocation((POINT *)&c, 10, 15);</pre><p>this is fine. ANSI/ISO 9899-1990 6.5.2.1 states that ``a pointer to astructure object, suitably converted, points to its initial member''.Hence, a pointer to a CIRCLE in your case can be suitably converted topoint to a POINT.<p>This question is becoming a more and more popular question lately.It may be time to update the FAQ. Comments and corrections to thefollowing are welcome.<p>Q. How can I implement object-oriented (OO) inheritance in C withstructures?<p>A. First, make sure that using an OO language (such as C++) is not agood option. The C language was not designed with OO in mind, andso facilities to support OO programming are limited. Think throughjust what OO features you will need, and think about how you expectto implement them in C.<p>That said, it is possible to implement single inheritance in C bynesting a structure inside of a structure. This techniques workssince a pointer to a structure object, when suitably converted,points to its initial member. Thus, single inheritance of a ``parent''structure is accomplished by creating a new ``child'' structure whoseinitial member is the same type as the ``parent''. The followingexample (thanks to Wayne O. Cochran) illustrates how this is done.<p><pre>typedef struct { int x; int y;} point;typedef struct { point center; int radius;} circle;void init_point(point *p, int x, int y){ p->x = x; p->y = y;}void init_circle(circle *c, int x, int y, int r){ init_point((void *)c, x, y); c->radius = r;}</pre><p>In this example, we see that `circle' inherits from `point' theinteger members `x' and `y'. Note that the following function isinvalid<p><pre>void bad_init_circle(circle *c, int x, int y, int r){ c->x = x; /* should be ((point *)c)->x = x; */ c->y = y; /* should be ((point *)c)->y = y; */ c->radius = r;}</pre><p>since the pointer to circle `c' has not yet been suitably convertedto point to the type of its first member.<p> References:<br>ANSI Sec. 3.5.2.1<br>ISO Sec. 6.5.2.1<p>-- <br>James C. Hu <jxh@cs.wustl.edu> Computer Science Doctoral Student <br><a href="http://www.cs.wustl.edu/~jxh/">http://www.cs.wustl.edu/~jxh/</a> Washington University in Saint Louis<br>>>>>>>>>>>>>> I use *SpamBeGone* <URL:<a href="http://www.internz.com/SpamBeGone/">http://www.internz.com/SpamBeGone/</a>></body><!-- Mirrored from c-faq.com/struct/oop.jxh.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:27 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -