C + + Basics easy to forget

Source: Internet
Author: User
Tags function prototype

1.const modifier : C language is accustomed to defining constants using # define, such as # define PI 3.14

C + + provides a more secure (where security is defined as a possible error when replacing), a more flexible way to define constants, with the const modifier, such as: const float pi=3.14; This constant PI is typed , It has an address that can be pointed to by a pointer, but it is not modified (so C + + is recommended to use const).

(1). pointer to constant : for example, const char* p= "ABC"; note that the value of the pointer p here can be changed, but the value of the constant that P points to cannot be changed. p[2]= ' x ';//Error

p= ' CDF ';//correct

(2). constant pointer : for example, char* const p= ' abc '; note that this cannot change the address that the pointer is pointing to, but the data in the address can be changed. p[2]= ' x ';//correct

p= ' CDF ';//Error

(3). constant pointers to constants : For example, const char* const p= ' abc ', where the pointer points to the address and the data in the address cannot be changed. p[2]= ' x ';//Error

p= ' CDF ';//Error

Description: 1. Once a constant is established, it cannot be changed anywhere in the program. 2. Unlike # define, Const can have its own data type and compile with good detection. 3. The function parameter can be modified with a const to ensure that the argument cannot be altered inside the function. that is, the argument is read-only, and if you want to find the maximum value of an integer array a[200] by I_max, you can define a function prototype:

int I_max (const int* PTR); The calling format enables: I_max (a);

2.void pointer : Void usually represents an indeterminate value, but when void is used as the type of a pointer, it represents a type that is indeterminate. This type of void pointer is a general-purpose pointer, meaning that any type of pointer value can be assigned to a pointer variable of type void.

void *p; declaring void type pointers

int i=256;

p=&i;

  Note that it is important to display the type conversion for a void pointer that has been given a value, and then to manipulate it. otherwise error!

...

cout<<* (int*) p<<endl;

...

3. inline function Inline: space-time, so usually only a small and frequently used function is defined as an inline function. Similar to the macro # #, but identical, it is recommended to use inline functions , more secure;

  (1). The member function of a class is defined in the class, and the default is an inline function, which defines a member function outside the class, and only the inline modifier is the inline function;

  (2). Inline functions can be defined more than once in a program, as long as inline functions appear only once in a. cpp file, and in all. cpp files, the inline function is defined the same way that it can be compiled. Obviously, it is wise to put the definition of an inline function into a header file. (This sentence turns from http://www.cnblogs.com/lidabo/archive/2012/04/17/2454568.html

4. function Overloading :

(1) when calling a function overload, the function return value type is not in the parameter checking column, so only the return value type is different, overloading is not allowed , error!

(2) When the function overload is used with the default worthy function, it is possible to cause ambiguity and error! Such as:

void Fun (int r=0,int x=0);

void Fun (int r);

When the call Fun (20) error, unrecognized is called which function, you can see that the function overload needs to be careful not to produce ambiguity !

5.New and delete:

The C language provides malloc and free for dynamic memory management, and C + + provides operator new and delete to do the same work, the latter better, recommending the latter !

Basic form: pointer variable name =new type; Delete pointer variable name;

int *p;

p=new int;

Delete p;

For arrays, the syntax is: pointer variable name =new type name [subscript expression];d elete [] pointer variable name;

int *p=new int[10];

int *pr=new int[2][3];

delete []p;

delete []PR;

Description: 1. operator new can automatically calculate the amount of memory to allocate, without sizeof (), to reduce errors. 2.new automatically returns the correct pointer type without forcing the type conversion. 3. But new and delete are used in pairs , otherwise the new application space becomes dead space! 4. general use of new when you want to check whether the allocation of memory success, not successful new return null pointer null (to form a good learning

customary);

6. References:

Concept: Reference is a C + + for a major supplement, the "reference" of the variable in C + + is the alias of the variable, the operation of the reference is the operation of the variable, both equivalent! So sometimes quoting

Clearer and simpler than pointers.

Format: type & Reference name = defined variable name;//the & here does not represent the fetch, which represents the reference.

  int a=5;

int &b=a;

Then B is a reference, and A and b represent the same variable, sharing the same inner deposit element!

C + + Basics easy to forget

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.