Simple inheritance mechanism for pure C language

Source: Internet
Author: User

0 Inheritance is the foundation of OO Design

Inheritance is the basic part of OO design, is also the foundation of realizing polymorphism, c++,c#,objective-c,java,php,javascript and so on OO design language, its language itself provides direct support for implementing inheritance. The language, which follows the C/unix design philosophy, never limits the programming style, and provides basic support for OO. Let's take a look at how to implement inheritance in C language.

1 The meaning of inheritance on the memory layout level

Almost all programmers today know the abstract meaning of inheritance and are familiar with examples of animals that have been used to the dogs. Here, we throw aside the abstract world and go deep into the concrete realization of inheritance. Of course, different languages do not have the same implementation mechanism for inheritance, but it is very beneficial to understand the inheritance by understanding one of the typical implementation details. Here we illustrate with C + + as an example.

class B{    int x;    int y;    int z;};class C : B{    float f;    char s[10];};

The code above indicates that subclass C inherits the parent Class B, and the following is the memory layout of an instance (object) of Class C.

The C object consists of two parts, and the red area is the part that inherits from B, and the blue area is unique to itself. In this way, the red part can be considered as a class B object entirely.

2 two ways to implement inheritance using structs 2.1 Parent class object as a member of a subclass

After understanding the principle of inherited memory layout, it is very easy to implement inheritance with C. The easiest way to think of this is as follows:

struct B{    int x;    int y;    int z;};struct C{    struct B objB;    float f;    char s[10];};

The above code implements inheritance by including a member of type B in C, which is very straightforward, but somewhat inconvenient to use.

    struct C objC;    10;    ((struct10;

To access the member X of the parent class, there are two methods, one is objc.objb.x, the other is (struct b*) &objc)->x = 10. Neither of these approaches seems straightforward enough. It is very frequent to access the members of the parent class in the subclass method.

void c_member_method(struct C* pObjC){    20/* 访问父类成员 */    0.23f/* 访问自身成员 */}

The first way, it feels more like OB style, rather than OO.
The second method, which must be forced type conversion, feels that the syntax is not aesthetically pleasing.

2.2 Subclass contains all the parent class members
struct C{    int x;    int y;    int z;    float f;    char s[10];};

All of the parent class members are treated as members of the subclass. This way the class object accesses the inherited members very directly.

void c_member_method(struct C* pObjC){    20/* 访问父类成员 */    0.23f/* 访问自身成员 */}void main(){    struct C objC;    10;}

Looks good, in fact there will be a big problem in engineering: difficult to maintain! For example, whenever you create a subclass, you must write all of the parent class members as they are, and the subclass needs to make the same changes when the parent class defines the changes. Once the parent class is slightly scaled, maintaining this inheritance will be a nightmare!

So how does that work out?
The method is out of the box, that is, the C-language preprocessing macro defines the # define. As shown below:

#define B_STRUCT \    int x;     int y;     int zstruct B{    B_STRUCT;};struct C{    B_STRUCT;    float f;    char s[10];};

This method can be replicated when the inheritance hierarchy is deeper, such as C inheritance B,d inheritance C.

#define B_STRUCT \    int x;     int y;     int zstruct B{    B_STRUCT;};#define C_STRUCT \    B_STRUCT;     float f;     char s[10]struct C{    C_STRUCT;};#define D_STRUCT \    C_STRUCT;     double dstruct D{    D_STRUCT;};

With macro definitions, this inheritance relationship can be easily implemented and maintained.

31-Point Thinking

Oo thought has dominated the software design, directly support OO programming language overwhelming, but OO there is no defect? How to use OO in moderation in your project?

Pure C language Implementation simple inheritance mechanism

Related Article

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: 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.