Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to rewrite C++ 's source code into C code

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to rewrite C++ 's source code into C code", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to rewrite the source code of C++ into C code.

C++ interpreter takes up more storage space than C language interpreter, so it wants to be compatible with C++ code on some specific occasions. At the same time, in order to save limited storage space, reduce costs, and improve efficiency, it is necessary to rewrite the source programs written in C++ language in C language.

The biggest difference between C++ and C is the concept and characteristics of classes in C++. The problem of changing C++ to C is transformed into the problem of how to de-classify.

There are two ways:

Remove the object-oriented features from C++, first understand all the logic of the source code, and then rewrite

It retains part of the object-oriented features in C # and uses structures to realize the function of the class.

The first method is fine when the number of classes is very small. If there are a large number of classes, it takes time to understand all the source code and then rewrite it, and it is easy to make mistakes. What's more, if you encounter a large project, it is almost impossible to fully understand the source code.

The following is a preliminary discussion on some features of C++ and how to implement or replace it in c.

Description:

The function Ixx is the implementation of the constructor of class xx.

The member function of the original class is changed to a function prefixed with the structure body name +'_'.

The declaration that the function pointer U is the destructor of the original class

The implementation of the destructor of the U+ structure whose name is the original class

The name of the Fun-_+ structure is to point to the pointer to the member function of the structure

The above situation will not be explained in the future.

First, the member functions and data members of the class

Because struct does not control the access rights of members, it is necessary to add additional mechanisms for access control, which complicates the program, so it can only give up the control of access rights.

The data members of the class can be converted directly to the data members of the structure in C.

Functions need to be converted to corresponding function pointers, because function declarations and definitions are not allowed in struct. Virture,inline and other modifiers should also be removed before the function, such as function void funca (int a); change to void (* funca) (struct B * pforce int a); you can see that the prototype of the function pointer adds a pointer to struct B, this is because to operate on the members of the class within the function, you have to specify the members of the structure by the pointer. In the member function of the class, there is actually an this pointer pointing to itself in the parameter column.

Static members are defined as global variables or global functions, because there can be no static members in the structure.

Second, the constructor of the class

The default constructor of the class is called when the class is instantiated. In struct, a function pointer with the same name is defined to point to an initialization function with a constructor function. Unlike the constructor, a statement for initialization of the function pointer is added to the initialization function. Use malloc instead of new when creating structural variables, and call the initialization function manually at this time.

As shown in the following example:

ClassA {public: a (); ~ A (); void func (int a); private: int b;}; typedef struct classA A; struct classA {void (* A) (struct classA * p); / / constructor pointer void (* U) (struct classA * p) / / destructor pointer void (* func) (struct classA * ppenint a); int b;}; void fun_A (A * p) {p-> func=classA_func; / / initialize the function pointer} void IA (A * p) / / constructor. The naming rules precede the class name with I {fun_A (p); p-> bend0. / / part of the original constructor} void classA_func (A * pscene int a) {p-> bauba;}

Use the following ways where it is used:

A * s = (A *) malloc (sizeof (A)); s-> A (A); s-> A (s)

Third, the destructor of the class

The job of the destructor of the class is to release the occupied resources.

In C, any struct replaces the destructor with a function pointer U. The reason why all struct use pointer U is based on the following situations:

If the subclass pointer is assigned to the base class pointer, the base class pointer does not have to consider which function name's destructor to call when it is released, just call the member function U. The member function U needs to be specified in the fun_ class name () function like a normal member function.

The destructor of the class is called by the system and is explicitly called in C. As for when to call, it should be judged accurately.

Fourth, copy constructor of class

The main purpose of the copy constructor of a class is to speed up the construction of a class in the following cases:

Passed to the function as an argument. (additem (Itema))

Returns a value as a function.

Instantiate a class as a parameter.

In all three cases, the copy constructor of the class is called directly by the system instead of the constructor.

Note: the copy constructor is not called by Cards D;, in which case methods that overload the'= 'operator are used. (see operator overloading for details)

Because the struct variable is defined in C, all pointers are used, and the copy constructor is not used, so I will not consider it for the time being. For the original function parameters or return values that require class variables, they should all be converted to class pointers. The case of instantiating a class as a parameter can be solved by defining another constructor with a parameter.

5. Inline functions and virtual functions of classes

The modifiers inline and virture of inline and virtual functions should be removed. The inline function body should be removed and the inline function is defined as a function on the outside. Such as:

Class B {… Virture void funb (); inline int add () const {return astatb;}; private: int a; int b;... }

Change to:

Typedef classB B; struct classB {... Void (* funb) (struct classB * p); int (* add) (struct classB * p); int a; int b;} void classB_funb (B * p) {… } int classB_add (B * p) {return p-> aquip-> b;} void fun_classB (B * p) {… P-> funb=classB_funb; p-> add= classB_add;}

VI. Heavy loading

There are two types of overloaded functions and operators in the class:

1) overloading of function

The condition of function overloading is that the function name is the same, the number of parameters or the type of parameters are different.

In this way, different functions will be called according to the parameters you enter.

We have to give different names in C, and there is no other solution.

2) operator overloading

Operator overloading is just to satisfy the general habit of using operators without errors.

Operator overloading is not supported in C, you can define a function to implement this function.

This is a modification of the general class.

VII. Inheritance of classes

1) single inheritance

If there is an inheritance relationship between classes, first modify the base class according to the modification of the general class. Then copy all the definition parts of the base class to the front of the subclass. In addition to changing the constructor name of the base class to the subclass constructor name, no other changes can be made to the part of the base class definition. And call the constructor of the base class in the constructor, and then redirect the function pointer to the subclass function if the subclass overrides the function of the base class. This is to maintain the dynamic binding features brought about by class inheritance.

The inheritance relationship between classes is complex and changeable. In order to ensure that the base class is unique and easy to modify among all subclasses, the best way is to make the structural part of the base class into a macro and use it directly in the subclass.

2) multiple inheritance

Personally, I think it is best not to use more inheritance, it will bring some problems, there will be multiple inheritance path problems. Unless it is used to facilitate programming, such as inheritance interfaces, and so on.

Multi-inheritance can also be changed, copy all the members of multiple base classes to subclasses, and if you encounter duplicate member names, add a prefix to distinguish them. Of course, this means that there is the same between the base classes. If there is a duplicate name between the derived class and the base class, the base class will be overwritten.

At this point, I believe you have a deeper understanding of "how to rewrite C++ 's source code into C code". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report