[Code] The difference between C language and C ++ language

This article introduces important or easily overlooked differences between C and C ++ . Although C ++ is almost a superset of C, C / C ++ code mixing is generally no problem, but understanding the important differences between C / C ++ can avoid some strange bugs.
If you are an OIer with C as the main language, this article will also let you get started with C ++ more smoothly.

Difference 1-Macro and Template

One of the uses of C ++ templates at the beginning of design is to replace macro definitions. Learning template programming is an important step from C to C ++. The template is different from the text replacement of the macro, and it will get a more comprehensive compiler check during compilation, which is convenient for writing more robust code, and the inline keyword can also obtain the compiler's full optimization. The template feature supports variable-length template parameter tables after C ++ 11, which can be used to replace variable-length functions in C and ensure type safety.

Difference 2-pointer and reference

You can still use C-style pointers in C ++, but for variable passing, it is more recommended to use the C ++ reference feature to achieve similar functions. Since the object pointed to by the reference cannot be null, it can avoid some problems of null address access. However, due to its flexibility, the pointer still has its use. It is worth mentioning that, C is NULLa null pointer has a type-safe alternatives in C ++ nullptr. Between pointers and by reference *and &are interchangeable operators.

Difference 3-struct

Although both C and C ++ have the concept of struct, their corresponding things cannot be mixed! The struct in C is used to describe a fixed memory organization structure , and the struct in C ++ is a class . The only difference between it and the class is that its members and inheritance behavior are public by default , while the default members of general classes are private. This is especially fatal when writing mixed C / C ++ code.

In addition, when declaring struct, C ++ does not need to be as cumbersome as C, C version:

typedef struct Node_t{
	struct Node_t *next;
	int key;
} Node;

C ++ version

struct Node {
	Node *next;
	int key;
};

Difference 4-const

const only has the function of restricting the variable that cannot be modified in C, and in C ++, due to the emergence of a large number of new features, const is also given more usage. The successor of const in C to C ++ is constexpr.

Difference 5-memory allocation

C ++ in a new newand deletekeywords used to allocate space on the "free store", this can be a free store heap may also be a static storage area, is in line with their "class" and that appears. Which delete[]can also direct the release of dynamic memory array, very convenient. newAnd deletekeywords will call the type constructor and destructor, compared to the C malloc(), realloc(), free()function, they have better support type, but less efficient than those in C functions.

In short, if you need to dynamically allocate memory object is the basis of their type or an array, then you can use malloc()a more efficient memory allocation; but if you create a new type of non-object-based, it is recommended to use newin order to obtain security an examination. It is noteworthy that although newand malloc()are returned pointer, but newout of the pointer can only use deleterecycling, and malloc()out of the hands can only use free()recycled, otherwise there is the risk of memory leaks.

Published 106 original articles · praised 156 · 40,000+ views

Guess you like

Origin blog.csdn.net/Ljnoit/article/details/104835113