How to change C ++ to C

xiaoxiao2021-03-06  36

How to change C to C

I have participated in the company's BPP project, which is Bluetooth Print Profile. Due to the open source package of HPIJS, it is C . Since the C interpreter takes about 500K more storage space occupied by the C language interpreter. In order to save a limited storage space, reduce costs, but also to improve efficiency, it is necessary to rewrite the source program written in C language.

The largest of C and C is the concept and characteristics of the classes in C , and the problem with C is changed to C, which is converted to how to make the problem. There are two ways: the first is to remove object-oriented features in C , first understand the logic of source code, and then rewrite; the second is to retain object-oriented partial features in C, implement the function of the class with structures . The first method, for the case of fewer numbers, if the number of classes is more, all understand the source code, and then overwrite very time, and it is easy to make mistakes, more, if you encounter big The project wants that it is almost impossible to understand the source code. There are more than 140 categories in the HPIJS program. At this time, the second method is needed. You can change the difficulty in a class, if it is not a pen error, it is hard to make mistakes, but also don't need to understand Program logic, maybe after you finish it, you don't know anything about the program. I don't say that I don't know what to do, just want to say that this method is independent of the program logic itself.

The following is some of the features of C , and how to implement or replace in C, do some preliminary discussions:

Description:

Function IXX is the implementation of the constructor of the class XX.

The original class member function is changed to a function of structural name '_'.

The function pointer u is the statement of the destructor of the original class;

U structural name is the realization of the destructor of the original class;

The FUN_ structure is pointed to the structural member function pointer;

It will not be described later in the above case.

One. Class member function and data member

Since Struct does not control the members' access rights, additional mechanisms must be added to access control, so that the program is complicated, so it can only discard access rights.

1) For data members of the class, it can directly turn directly to the data member of the structure of C.

2) The function needs to be converted to the corresponding function pointer because the function declaration and definition is not allowed in Struct. If there is Virture, Inline and other modifiers before the function, such as the function Void Funca (INT A); change to Void (* FUNCA) (Struct B * P, Int a); everyone can see the prototype of the function pointer The pointer of a pointer STRUCT B is because the members of the class are to be operated in the function, and the member of the structure is specified by the pointer. In the member function of the class, it is actually implied in the parameter column to point to its own THIS pointer.

3) For static members, they must be defined as global variables or global functions because there is no static member in the structure.

two. Class constructor

Categories When instantiation, the default constructor is called, in the STRUCT, to define a function of the same name function pointer pointing to an initialization function with constructive function, is different from the constructor, to join the function in the initialization function The statement initialized by the pointer. When you use Malloc instead of new, you should manually call the initialization function.

As shown in the following example:

Class a {

PUBLIC:

A ();

~ A ();

Void Func (INT A);

Private:

INT B;

}

A :: A ()

{

B = 0;

}

Void A :: Func (INT A)

{

B = a;

}

Typedef struct classa a;

Struct Classa

{

Void (* a) (struct classa * p); // Construct function pointer

Void (* U) (Struct Classa * P); // Destructor Pointer

Void (* func) (struct classa * p, int a);

INT B;

}

Void fun_a (a * p)

{

P-> func = classa_func; // initialize the function pointer

}

Void IA (A * P) // Constructor, naming rules plus i before class name

{

Fun_a (p);

P-> b = 0; / / part of the original function function

}

Void classa_func (a * p, int A)

{

P-> b = a;

}

The following places are used as follows:

A * s = (a *) Malloc (SizeOf (a));

S-> a = IA;

S-> a (s);

three. Class destructor

The work made by the sect of the class is to release the resources accounted for.

In C, no matter which structure uses a function pointer u to replace the destructor. All structs are based on the following cases:

If the subcategory pointer is assigned to the base class pointer, the base class pointer does not have to consider the destructor of which function name is called when it is released, simply call the member function U. Members function u You need to specify in the FUN_ class name () function like a general member function.

The destructor of the class is called by the system, and it is explicitly called in C. As for when it is called, it is necessary to accurately determine.

four. Class copy constructor

The primary use of the copy constructor of the class is to speed up the construction speed under the following situations:

1. As a parameter transmission function. (AddItem (item))

2. Return the value as a function.

3. Make parameters when instantiated.

In these three cases, the copy constructor of the system directly calls the class is instead of the constructor.

Note: c = d; Whether the copy constructor is not called, in which case the method of overloading the '=' operator is used. (See operator overload);

When defining the Struct variable in c, all of the pointers are used, and the copy constructor is not used, so it is not considered. For the original function parameters or the return value requires a class variable, it is necessary to translate into a pointer. When instantiating the class, the parameter is used, and a configuration function with parameters can be defined.

Fives. Inline function and virtual function

The inline function and the modifier inline of the virtual function, Virture wants all. The inline function is removed, and the inline function is defined outside as a function. Such as:

Class B

{

...

Virture void funb ();

Inline int Add () const {return A b;};

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-> A P-> B;

}

Void fun_classb (b * p)

{

...

P-> funb = classb_funb; p-> add = classb_add;

}

six. Overload

The overloaded function overload and operator are overloaded in the class:

1) Overload of the function

The condition of the function overload satisfaction is: the same function name, the number of parameters, or the parameter type.

This will call different functions based on the parameters you entered.

There is no different names in C, there is no other solution.

2) Operator overload

The operator is only used to meet the habits used by the general operator, and there will be no errors.

Calculator overload is not supported in c, which can define a function to implement this function.

This is a modification of the general class.

Seven. Inheritance

1) Single inheritance

If there is a inheritance relationship between the class, the base class will be modified according to the general class. Then copy all of the defined parts of the base class to the front of the subclass. In addition to converting the constructor name of the base class to a subclass constructor, the part defined by the base class cannot be other modified. And call the constructor of the base class in the constructor, and then if the subclass overwrow the function of the base class, the function pointer will be reached to the subclass function. This is the characteristic of the dynamic cable brought by the inheritance of the class.

The inheritance relationship between classes is complex and varied. In order to ensure the only and convenient modification of the base class in all subclasses, the best way to make the structural part of the base class into a macro, directly in the subclass. I.e.

2) Multiple inheritance

I personally think that more inheritance is best not to use, he will bring some problems, there will be a number of inheritance paths. Unless it is used for convenience of programming, such as inheritance interface, etc.

Many inheritance can also be changed, copy all members of multiple base classes to the subclass, encounter repetitive member names, then the front plus the prefixed difference, of course, this is the same, the base class is the same, If there is a rendering between the derived class and the base class, the base class will be overwritten.

Eight. other

The above is the main difference and the most common characteristics and modifications of C . Other more than the use of templates, etc., these are for convenience of programming, multiplexing code. There is no in c, you have to write multiple functions yourself. There are also the & symbols in the parameter list to replace the pointer, the default value is also removed, and should pay attention to the default value when calling.

转载请注明原文地址:https://www.9cbs.com/read-75718.html

New Post(0)