Ansi c and object-oriented programming

Source: Internet
Author: User
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 versions ages such as C ++ and Java provide extends features that encourage this style of design, you can benefit equally well from the use of objects in imperative versions ages 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 shoshould 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.

C code:
Const   Char * Httprequest_getheadervalue (httprequest * O, Const   Char * Name );
C ++ Code:
Const   Char * Httprequest: getheadervalue ( Const   Char * Name );

We use the notation "O" as the "Object Pointer" in the C code, which is equivalent to the automatic ic "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.

Typedef Struct
{
Httppage super;/**//*As if inherited*/
IntMyownvar;
} Mypage;

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.

Typedef Struct
{
IntMyownvar;
Httppage super;/**//*Ooops*/
} Mynonworkingpage;

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

C code:
Typedef Struct
{
Httppage super;/**//*As if inherited*/
IntMyownvar;
} Mypage;

C ++ Code Is Memory compatible with abve code:
Class Mypage: Public Httppage
{
IntMyownvar;
} ;

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.

Mypage_constructor (mypage * O)
{
/**/ /*It is safe to typecast mypage to httppage since httppage is the first
* Aggregate in mypage. We have to explisitly downcast mypage to httppage
* In C code. c ++ wocould implisitly downcast the type.
*/
Httppage_constructor (httppage * ) O, mypage_service, " Mypage " );
}

The second argument, mypage_service, is the page service function. in a pure C ++ environment, one wocould 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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.