ANSI C and object-oriented programming

ANSI C and object-oriented programming

Do you think that to program in an object-oriented style you need an object-oriented language? Well, you're wrong. It seems to be a common myth that you need an object-oriented language to implement an object-oriented design and, although languages such as C++ and Java provide many features that encourage this style of design, you can benefit equally well from the use of objects in imperative languages such as C.

The Web-Server uses no global data. All data is presented as objects to the user. The object type is a C struct. Each object type has a number of member functions or methods associated with the object. Accessing the data in the objects is done via these member functions. You should never directly access the data in the C struct. The member functions starts with the same name as the type. For example, class HttpRequest has a member function getHeaderValue. The fully qualified C function name will therefore be HttpRequest_getHeaderValue and the C++ name will be HttpRequest::getHeaderValue.

All C member functions take a pointer to an instance of the type as the first argument. For example, the HttpRequest_getHeaderValue takes the HttpRequest* type as the first argument. You use the "this" pointer in C++, thus you do not explicitly pass in the pointer to the class itself.

None.gif C code:
None.gif
const   char *  HttpRequest_getHeaderValue(HttpRequest *  o,  const   char *  name);
None.gifC
++  code:
None.gif
const   char *  HttpRequest::getHeaderValue( const   char *  name);
None.gif

We use the notation "o" as the "object pointer" in the C code, which is equivalent to the automatic "this" pointer in C++.

Some of the classes in Barracuda are abstract classes, which can be seen as a protocol between the Web-Server and the application code. You must inherit and implement the functionality of the abstract class before registering an instance of the class with the Web-Server. Such a class is HttpPage.

None.gif typedef  struct
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif   HttpPage super; 
/**//* As if inherited */
InBlock.gif   
int myOwnVar;
ExpandedBlockEnd.gif}
 MyPage;
None.gif

The HttpPage object is actually an aggregate of the MyPage type. It is important that HttpPage is the first aggregate in the struct. For example, the following code will not work.

None.gif typedef  struct
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
int myOwnVar;
ExpandedSubBlockStart.gifContractedSubBlock.gif   HttpPage super; 
/**//* Ooops */
ExpandedBlockEnd.gif}
 MyNonWorkingPage;
None.gif

A C++ compilers will be memory compatible with the C code if you write:

None.gif C code:
None.giftypedef 
struct
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif   HttpPage super; 
/**//* As if inherited */
InBlock.gif   
int myOwnVar;
ExpandedBlockEnd.gif}
 MyPage;
None.gif
None.gifC
++  code  is  memory compatible with above code:
None.gif
class  MyPage :  public  HttpPage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
int myOwnVar;
ExpandedBlockEnd.gif}

None.gif

In C++ a constructor is used to initialize an instance of a class. We use the notation "struct name" followed by the name "_constructor"; for example, the HttpPage C initialization function name is HttpPage_constructor and the C++ version is HttpPage::HttpPage. You must explisitly call the constructor as a function when writing C code. The following code example shows how to write a constructor for the MyPage class.

None.gif MyPage_constructor(MyPage  *  o)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//* It is safe to typecast MyPage to HttpPage since HttpPage is the first
InBlock.gif    * aggregate in MyPage. We have to explisitly downcast MyPage to HttpPage
InBlock.gif    * in C code. C++ would implisitly downcast the type.
ExpandedSubBlockEnd.gif    
*/

InBlock.gif   HttpPage_constructor((HttpPage
*)o,MyPage_service, "MyPage");
ExpandedBlockEnd.gif}

None.gif

The second argument, MyPage_service, is the page service function. In a pure C++ environment, one would typically use virtual functions for overloading the behavior in the super class, but virtual functions cannot be used by C code, thus C function pointers must be used in both C and C++ code.

Other classes such as the HttpDir class can be used as is, or can be overloaded, that is, the functionality of the HttpDir class can be extended or customized. Using function pointers does this and one can replace the default service function in the HttpDir with a customized service function.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
没有任何一种编程技术能解决所有问题; 没有任何一种编程语言只输出正确的结果; 没有任何一个程序员需要从零开始一个项目。 -----这三句似曾相识? 这是一本17年前的书,写的是用纯C来实现OOP。。。 亘古的C,永远的C。。。 关天C,我没有其它要说的了。 关于这个资源,还有一句:原书+源码,全在这儿 我希望10年后我还在读它,看它。。。 No programming technique solves all problems. No programming language produces only correct results. No programmer should start each project from scratch. Object-oriented programming is the current cure-all — although it has been around for much more then ten years. At the core, there is little more to it then finally applying the good programming principles which we have been taught for more then twenty years. C++ (Eiffel, Oberon-2, Smalltalk ... take your pick) is the New Language because it is object-oriented — although you need not use it that way if you do not want to (or know how to), and it turns out that you can do just as well with plain ANSI-C. Only object-orientation permits code reuse between projects — although the idea of subroutines is as old as computers and good programmers always carried their toolkits and libraries with them. This book is not going to praise object-oriented programming or condemn the Old Way. We are simply going to use ANSI-C to discover how object-oriented programming is done, what its techniques are, why they help us solve bigger problems, and how we harness generality and program to catch mistakes earlier. Along the way we encounter all the jargon — classes, inheritance, instances, linkage, methods, objects, polymorphisms, and more — but we take it out of the realm of magic and see how it translates into the things we have known and done all along. I had fun discovering that ANSI-C is a full-scale object-oriented language. To share this fun you need to be reasonably fluent in ANSI-C to begin with — feeling comfortable with structures, pointers, prototypes, and function pointers is a must. Working through the book you will encounter all the newspeak — according to Orwell and Webster a language ‘‘designed to diminish the range of thought’’ — and I will try to

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值