⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oopc.html

📁 c语言是面向过程的程序语言
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"><html><head>   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">   <meta name="Author" content="Laurent Deniau">   <meta name="GENERATOR" content="Mozilla/4.76 [en] (X11; U; Linux 2.2.17 i686) [Netscape]">   <title>Object Oriented Programming in C</title></head><body text="#000000" bgcolor="#C8C8C8" link="#0000EF" vlink="#51188E" alink="#FF0000">&nbsp;<table CELLPADDING=10 COLS=1 WIDTH="100%" BGCOLOR="#FFC6AC" NOSAVE ><tr NOSAVE><td NOSAVE><center><b><font face="Arial,Helvetica"><font size=+3>Object Oriented Programmingin C</font></font></b></center></td></tr></table><center><p><b><font size=+1><a href="mailto:Laurent.Deniau@cern.ch">Laurent Deniau</a></font></b><p>March 10, 2001<br>revised May 17, 2001<p>[ <a href="#Introduction">Introduction</a> | <a href="#Object_Model">ObjectModel</a><br><a href="#Object">Object</a> | <a href="#Class">Class</a> |<a href="#Messages">Messages</a>| <a href="#Methods">Methods</a> | <a href="#Encapsulation">Encapsulation</a>| <a href="#Interface">Interface</a> | <a href="#Implementation">Implementation</a><br>&nbsp;<a href="#Inheritance">Inheritance</a> | <a href="#Multiple_Inheritance">MultipleInheritance</a> | <a href="#Polymorphism">Polymorphism</a> | <a href="#Abstract_Class">AbstractClass</a> | <a href="#Genericity">Genericity</a> | <a href="#Exceptions">Exceptions</a><br><a href="#Debugging">Debugging</a> | <a href="#ISO_C99">ISO C99</a>| <a href="#Performances">Performances</a> | <a href="#Keywords">Keywords</a>| <a href="#Examples">Examples</a> |<a href="#References">References</a>| <a href="#Mailing_List">Mailing List</a> | <a href="#ChangeLog">ChangeLog</a>]</center><p><br><table CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#66FFFF" NOSAVE ><tr NOSAVE><td NOSAVE><a NAME="Introduction"></a><b><font face="Arial,Helvetica"><font size=+2>Introduction</font></font></b></td></tr></table><p>This paper presents some programming techniques that allow large projectsbased on ISO C89 to get the benefits of object oriented design. It doesn'tintend to be a course on OOP techniques and assumes the reader to havea good knowledge of the C language. Since OOPC is based on the <i>C++ ObjectModel</i>, a good knowledge of C++ may help to understanding it better.These techniques may be useful for programmers who have not a C++ compilerfor their architecture (calculators, small systems, embedded systems).It may also be useful for people who are disappointed by C++ compilerswhich do not behave like the norm says or even do not support all the C++features or by C++ APIs that change from time to time. In fact, I don'tknow (at the revised date of this paper) any compiler which fully supportthe norm C++98. It is clear that the techniques presented here have notthe pretension to replace C++, that is impossible without a <i>cfront</i>translator (OOPC uses only C macros and few C lines), but it provides enoughto do serious OOP:<ul><li>The <i>procedural model</i> using C functions...</li><li>The <i>abstract data type model</i> using public interface and privateimplementation as well as data and names encapsulation.</li><li>The <i>object-oriented model</i> using (multiple) inheritance and polymorphismwhich allows to manipulate different object types through a common interface.</li></ul>OOPC also provides mechanisms to handle easily:<ul><li>Genericity (C++: templates).</li><li>Exceptions (C++: exceptions, <tt>try</tt>, <tt>catch</tt>, <tt>throw</tt>,<tt>auto_ptr</tt>).</li><li>Debugging (dynamic allocation, function call, object construction).</li></ul>Comparing to the <i>C++ Object Model</i>, OOPC does not provide:<ul><li>Automatic object construction and destruction (must be explicitly called).</li><li>Automatic array of objects construction and destruction (must be done byuser).</li><li>Non-polymorphic objects (all objects are polymorphic).</li><li>Virtual inheritance (too complex and not essential).</li><li>Pointer to virtual member function handling polymorphism (too complex orslow).</li><li>And more...</li></ul>Beside the advantages of the OOPC, one important constraint is to keep<b>typechecking everywhere</b> to ensure code quality and good debugging and thereforeit forbids the extensive use of untyped pointers (<tt>void*</tt>) for polymorphismpurposes. Another constraint is to provide a mechanism for name encapsulationto allow same method to be applied to different objects while C89 doesnot support function overloading. Finally we must provide a mechanism toallow preprocessor macros to accept a variable number of arguments likethe C99<tt>__VA_ARGS__</tt> predefined macro does. In the following, wewill use some terminology borrowed&nbsp; from C++ (and Objective C) butwith a slight difference about the meaning of <i>object</i> (C++: classobject) and <i>class</i> (C++: class). To avoid any confusion, this paperwill use the following therminology:<ul><li>A <i>method</i> is a member function (C++: member function).</li><li>An <i>object method</i> (message) is a polymorphic member function (C++:virtual member function = method).</li><li>A <i>class method</i> is a member function not working on an object (C++:static member function).</li><li>An <i>object</i> includes all the non-static data plus a hidden pointer(C++: class object with methods).</li><li>A<i> virtual table</i> includes all the object methods (C++: virtual table).</li><li>A <i>class</i> includes all the static data, methods and class methods(C++: class).</li></ul>To use the proposed OOPC techniques, you need to include the header files<tt><a href="ooc.h">ooc.h</a></tt>(depends on <tt><a href="exception.h">exception.h</a></tt> and <tt><a href="ooexception.h">ooexception.h</a></tt>)in your project and to link it with the <tt><a href="ooc.c">ooc.c</a></tt>and <tt><a href="exception.c">exception.c</a></tt> files. If you want todebug your OOPC program, you need to include the header file <tt><a href="oodebug.h">oodebug.h</a></tt>and to link your project with <tt><a href="oodebug.c">oodebug.c</a></tt>.To be informed about the OOPC evolution and discussion you can subscribeto the <a href="mailto:forum-oopc@listbox.cern.ch">forum-oopc</a> mailinglist (see <a href="#Mailing_List">Mailing List</a>).<br>&nbsp;<table CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#66FFFF" NOSAVE ><tr NOSAVE><td NOSAVE><a NAME="Object_Model"></a><b><font face="Arial,Helvetica"><font size=+2>ObjectModel</font></font></b></td></tr></table><p>OOPC is based on the <i>Object-Oriented Model</i> described in thissection and strongly inspired from the <i>C++ Object Model</i>. If youare not familliar or simply not interested by object model, you can skipthis section although reading it may greatly help for the understandingof the behaviour of OOPC and even of C++.<p>This model is built around three different types available for eachclass plus a general RTTI (Run-Time Type Information) type:<ul><li>The <i>object</i> collects the class non-static data (any instances).</li><li>The <i>class</i> collects the class static data, class methods and methods(one instance, doesn't exist in C++).</li><li>The <i>virtual table</i> collects the class object methods (one instance).</li><li>The <i>type info</i> collects the class RTTI (one intance).</li></ul><center><table BORDER WIDTH="100%" NOSAVE ><caption><b>Object Model Representation</b></caption><tr NOSAVE><td ALIGN=LEFT VALIGN=TOP NOWRAP BGCOLOR="#99FFCC" NOSAVE><tt>object&nbsp;&nbsp;&nbsp;&nbsp;(*)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vtbl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type_info&nbsp; (1)</tt><br><tt>+----------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +----------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+----------+</tt><br><tt>|&nbsp;&nbsp; __vptr | ---+-> |&nbsp;&nbsp; __info | -----> |&nbsp;&nbsp;__name |</tt><br><tt>+----------+&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; | __offset |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp; __class |</tt><br><tt>|[data]&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; +----------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp; __super |</tt><br><tt>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp; |[methods] |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;__extra |</tt><br><tt>+----------+&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | __offset |</tt><br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp; +----------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +----------+</tt><br><tt>class&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (1)&nbsp; |</tt><br><tt>+----------+&nbsp;&nbsp;&nbsp; |</tt><br><tt>|&nbsp;&nbsp; __vptr | ---+</tt><br><tt>|&nbsp;&nbsp; ctor() |</tt><br><tt>|&nbsp;&nbsp; dtor() |</tt><br><tt>|&nbsp;&nbsp; ator() |</tt><br><tt>+----------+</tt><br><tt>|[methods] |</tt><br><tt>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |</tt><br><tt>+----------+</tt></td></tr></table></center><p>The methods in the previous representation can be classified as follow:<ul><li>Methods in the <tt>vtbl</tt> are <i>object methods</i> (C++: virtual memberfunctions).</li><li>Methods in the <tt>class</tt> working on an object are <i>methods</i> (C++:member functions).</li><li>Methods in the <tt>class</tt> not working on an object are <i>class methods</i>(C++: static member functions).</li></ul>The <tt>ctor()</tt> has always the same name as the class and thereforeit is always called by <tt><i>class</i>.<i>class</i>()</tt> (C++: defaultconstructor <tt><i>class</i>::<i>class</i>()</tt>). The <tt>dtor()</tt>has always the same name as the class name preceded by an underscore (C++:tilde) and therefore is always called by <tt><i>class</i>._<i>class</i>()</tt>(C++: default destructor <tt><i>class</i>::~<i>class</i>()</tt>). The <tt>ator()</tt>is always called <tt>alloc()</tt> (C++: default allocator <tt>new(<i>class</i>)</tt>)and may throw <tt>ooc_bad_alloc</tt> exception upon failure. In OOPC, thedefault constructor&nbsp; and allocator are defined, declared and generatedsince they return only a well formed instance of the object without anydynamic initialization (only static initializations can be performed),but the default destructor is only defined and declared (not generated)since it may require some dymanic freeing and therefore needs to be providedby the class designer (even if empty).<p>The <tt>__vptr</tt> in <tt>object</tt> points to the virtual table andallows to call object methods without knowing its type. This is the keypoint of the polymorphism mechanism. It also allows to reach the classRTTI data required for dynamic casting.<p>The <tt>__vptr</tt> in <tt>class</tt> also points to the virtual tableand allows to bypass the polymorphism mechanism. It also allows classesto behave like objects (with some limitations) for object methods callsand RTTI purposes.<p><b>Object Model and Single Inheritance</b><p>The principle used for single inheritance is the structure mapping,that means if <tt>object2</tt> inherits from <tt>object1</tt>, the dataof object1 are at the beginning of <tt>object2</tt>, and therefore an <tt>object2</tt>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -