Writing high-quality code to improve C + + program syntax 2< changes from C to C + + >

Source: Internet
Author: User

Accidental Discovery Network reading channel, http://book.51cto.com/art/201202/317549.htm, very good Hope csdn also have, may already have ...

Read the "Writing high-quality code-improve C + + program 150 Recommendations", summarized and summarized;

This article is used to deepen memory and urge the purpose of learning.


0 How C code is used in C + +

such as: int func (int a, char b);

c compile function without the type information of the function, the above function is compiled into a symbol similar to _func

C + + in order to implement overloading, at compile time will be added to the function parameter type, the above function will be compiled into a symbol like _func_int_char

In order to resolve the above contradictions, you can take the following 3 ways:

1) Modify the C code header file, when it contains C + + code, add extern "C" in the Declaration


/*c Language header file: cdemo.h*/#ifndef c_src_demo_h#define c_src_demo_hextern "C" int Func (int x,int y); #endif/*c implementation file: cdemo.c*/# Include "CDemo.h" int Func (int x,int y) {    ...} Call # include "CDemo.h" int main () {    Func (1,6) in C + +;    return 0;}
2) Re-declare the following C function in C + + code and add the extern "C" when re-declaring

/*c Language header file: cdemo.h*/#ifndef c_src_demo_h#define c_src_demo_hextern int Func (int x,int y); #endif/*c implementation file: cdemo.c*/# Include "CDemo.h" int Func (int x,int y) {    ...} Call # include "CDemo.h" extern "C" int Func (int x,int y) in C + +; int main () {    func (1,6);    return 0;}
3) Add the extern "C" when the C header file is included

/*c Language header file: cdemo.h*/#ifndef c_src_demo_h#define c_src_demo_hextern int Func (int x,int y); #endif/*c implementation file: cdemo.c*/# Include "CDemo.h" int Func (int x,int y) {    ...} In C + +, call extern "C" {    #include "CDemo.h"}int Main () {    Func (1,6);    return 0;}



1 careful use of functions of the memcpy series

memcpy, Memset, MEMCMP series functions for C-style data type introduction security

However, because of the polymorphism of C + +, when using virtual functions, each inheritance of a class produces a virtual function table, which holds pointers to virtual functions, which must be stored in the object body, that is, with the object's data, so that the object in memory is not stored in a continuous way, Therefore, using the MEMCPY series function can cause unpredictable errors.


2 try to use New/delete instead of Malloc/free

The difference between Mollac and new:

1) New is the C + + operator, and malloc is the standard library function of C

2) New creates something with a type, and malloc returns void*, which requires coercion of type conversion

3) New can automatically call the object's constructor, and malloc does not

4) New failure will call New_handler handler, malloc failure will return null directly

Free and delete

Delete is a C + + operator, and free is a library function of C

Delete can automatically call the object's destructor, and free does not


3 flexible use of different styles of annotations

2 different annotation methods://For comment line,/* and/* Combination comment All the code between the two


4 try to use C + + standard iostream

It is recommended to use # include <iostream> instead #include <iostream.h>,#include <stdio.h>, #include <cstdio>



5 try to use C + + style forced type conversions

Const_cast<T*> (a) static_cast<T*> (a) reiniterpret_cast<T*> (a) dynamic_cast<T*> (a)


6 try to replace # define with const, enum, and inline

First steal will be lazy, hey, http://book.51cto.com/art/201202/317613.htm


7 using references instead of pointers

★ Same point:

1. Is the concept of the address;
The pointer points to a piece of memory whose contents are the address of the referred memory;
A reference is an alias for a block of memory.

★ Difference:

1. The pointer is an entity, and the reference is only an individual name;
2. The reference does not need to dereference (*), the pointer needs to be dereferenced;
3. References can only be initialized once at the time of definition, and then immutable; pointers are variable;
Reference "mindedness" ^_^
4. Reference does not have a const, pointer has const,const pointer is immutable;
5. The reference cannot be null, the pointer can be empty;
6. "sizeof Reference" gets the size of the variable (object) pointed to, and the "sizeof pointer" gets the size of the pointer itself (the address of the variable or object to which it is pointing);
typeID (t) = = typeid (t&) constant is true, sizeof (t) = sizeof (t&) constant is true, but when referenced as a class member name, it occupies 4 bytes of space with the pointer (no standard rule found).
7. The pointer and the reference self-increment (+ +) operation has different meanings;


Writing high-quality code to improve C + + program syntax 2< changes from C to C + + >

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