1. Const in C + +
(1), theconst in C + + is a true constant, you can define the size of the array;
The code is as Follows:
#include <iostream>using namespace std; #define A 10int main (void) {int A = 10; int array[a]; The Linux kernel supports this definition of arrays, and the other compilers do not support const int a = 10; Here is the constant, so you can make the following array size determination; int array[a]; Any platform is right, const compiles, executes; Return 0;}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8A/AF/wKiom1g3FjfxOSt5AAAoSq71vYA230.png-wh_500x0-wm_3 -wmp_4-s_30531414.png "title=" qq20161125003251.png "alt=" wkiom1g3fjfxost5aaaosq71vya230.png-wh_50 "/>
(2),C + + is often quoted, must open up a new memory space;
The code is as Follows:
#include <iostream>using namespace Std;int main (void) {//int &b = 10;//normal reference, reference a constant, constant no memory address, reference is to give inside access to multiple aliases; const int &B = 10; The C + + compiler allocates memory space for it;}
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8A/AB/wKioL1g3F9Oxpcf1AAAgXXfpYfc261.png-wh_500x0-wm_3 -wmp_4-s_1730048027.png "title=" qq20161125003939.png "alt=" wkiol1g3f9oxpcf1aaagxxfpyfc261.png-wh_50 "/>
(3), the nature of the const modifier after the function: void fun (int a) const;
essence: void Fun (const Teacher * Const this, int a) {}, which is decorated with *this;
2, the nature of the reference
The nature of the reference: the internal implementation of the reference in C + + is a constant pointer; The C + + compiler took an address for our programmer;
Type &name <===> type * Const name;
(1), the code is as Follows:
#include <iostream>using namespace Std;struct Teacher{char name[64]; int age; int &a; Much like the amount of memory space the pointer occupies; int &b; };//ordinary reference occupies memory space, and the size of the pointer occupies the same amount of space;//the nature of the reference: the internal implementation of the reference in C + + is a constant pointer; C + + compiler takes an address for our programmer;//type &name <===> type * Const Name;int main (void) {cout<<sizeof (Teacher) <<endl; Return 0;} The general int returns the value of the variable;//general int &, The reference returns the variable itself; the function can be placed on the left/right;//the return value of a reference is not a temporary space;//g2 () = 100; Can be used as an lvalue because the reference returns a contiguous amount of storage space, which is a variable; function as an lvalue, you must return a reference;
(2), Operation Result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8A/AB/wKioL1g3FubR5pkGAAAuPpNomSY918.png-wh_500x0-wm_3 -wmp_4-s_2198243423.png "title=" qq20161125003526.png "alt=" wkiol1g3fubr5pkgaaauppnomsy918.png-wh_50 "/>
Cause: because I'm using a 64-bit, the pointer is 8 bytes, and the byte alignment is the integer multiple of the largest underlying data type byte, which should be an integer multiple of 8;
3. Inline functions for C + +
(1), the inline function must be written with the implementation of the function body;
(2), the inline function directly inserts the function body in the function call place;
(3), inline Function faster, No pressure stack, reverse, return and other additional costs;
(4), the inline function can not have a circular statement, there can be too many conditional judgment statements;
(5), inline function is just a request, the C + + compiler does not necessarily allow such a request;
The code is as Follows:
#include <iostream>using namespace std;inline void PrintA () {//inline function allows C + + to do special processing; int a = 10; cout<< "a =" <<a<<endl;} The inline function replaces the macro code fragment; Inlineint main (void) {printA (); Return 0;}
4. Default parameters in C + +
(1), the code is as Follows:
#include <iostream>using namespace Std;//parameter is not passed, the default parameter is used; int PRINTFA (int a = 1) {cout<<a<<endl;} int printfa_1 (int a, int b, int c = 1) {}//c++ allows the function to have default Parameters//default parameter rules: starting from the left, if the default parameter appears, then the next (right) must be the default parameter; int printa_2 (int a = 1, int b = 2, int c) {}int main (void) {PRINTFA (); Return 0;}
(2), Operation Result:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8A/AB/wKioL1g3GbqgQJi3AAA_ZEcAYrI359.png-wh_500x0-wm_3 -wmp_4-s_3537223707.png "title=" qq20161125004735.png "alt=" wkiol1g3gbqgqji3aaa_zecayri359.png-wh_50 "/>
Default parameter rule: start from the left, if the default parameters appear, then the next (right) must be the default parameters, otherwise error!!
5. Placeholder parameters and default parameters
(1), the code is as Follows:
#include <iostream>using namespace Std;//function placeholder Parameter: when a function call, you must write enough arguments, void func1 (int a, int b, int) {cout<< "a" <&L t;a<< "b" <<b<<endl; }//default parameters and placeholder parameters//purpose: leave extensions for future programs; void Func2 (int a, int b, int = 0) {//this function is both a default parameter and a placeholder parameter; cout<< "a" <<a<< "b" <<b<<endl;} int main (void) {//func1 (1, 2); The placeholder parameter must now be 3, so error; Func1 (1, 2, 3); To Func2 (1, 2); Placeholder and default parameters, 2/3 parameters are all possible; Func2 (1, 2, 3);//at This time the 3rd parameter has no meaning, can not be used; Return 0;}
This article is from the "wait0804" blog, make sure to keep this source http://wait0804.blog.51cto.com/11586096/1876432
Const in C + +, referential nature, inline, default/placeholder parameters,