Boolean type, operator alias, C + + function, dynamic memory allocation (NEW\DELETE), reference (DAY02)

Source: Internet
Author: User

Six C + +the Boolean type1The bool type is a basic type in C + + and specifically represents a logical value:true/false2bool occupies one byte in memory: 1 means true,0 is false3The bool type can receive the result of any type and expression whose value is not 0 true, and a value of 0 is false seven operator alias (understanding)&&--" and|| --"or{--"<%}   --"%>... Eight C++function1function Overload strcpy (Char*,Const Char*) strcpy (Char*,Const Char*,int)1defined in the same scope, define functions with the same name, but their parameter tables must be distinguished, such that the function is an overloaded relationship. Note: Overloading and returning return type Independent2function overload matching calls the overloaded Relationship function function, and the compiler automatically chooses the optimal overloaded version based on the actual participation parameter matching degree. The current compiler's matching principle g+ + V4.8.1: Exact Match> Constant Conversion > Upgrade conversion > Downgrade conversions >ellipsis match3) function overloading principle C++The compiler integrates the information of the parameter table into the new function name by changing the name of the function, and realizes the contradiction between function overloading and name. Eg: the function written in the code:voidFuncintIDoubled); voidFuncinti); The function name changes after compilation: Func (int,Double)--_z4funcid func (int)--_z4funci Written question: add extern to the function declaration"C"role? Request C++The compiler does not change the name of the function, making it easier for the C program to call the function. Syntax:extern "C" voidfunc (..)  {..} ----------------------------extern "C"{      voidfunc1 () {}voidFunc2 () {}}-------2Default argument for function (default argument)1You can specify default values for some and all parameters of a function, call the function, and, if you do not give the argument, take the default value as the corresponding parameter value. Eg:voidFuncintAintBintflag=0){..} intMainvoid) {func (Ten, -,1); Func (Ten, -); }2The default parameter must be right, and if a parameter has a default value, all parameters on the right side of the parameter must have a default value. 3If the function is defined and declared separately, the default parameter should be written in the declaration portion of the function, while the definition section is not written. voidFunc () {.}--definition of a functionvoidFunc (); -declaration of a function3dummy parameters of a function1definition: A formal parameter that has only a type and no variable name is called a dummy eg:voidFuncintAint/*Dummy Element*/){}2use a dummy scene--"In order to be compatible with the old Code algorithm library:voidMath_func (intAintb) {...} User:intMainvoid) {Math_func (Ten, -); ... math_func ( -, -); }------------------------------------Upgrade Algorithm Library:voidMath_func (intAint=0){...}--"operator overload, distinguish before and after ++/--(later)4inline function (inline)//written question: the role of the inline keyword1defines a function that uses the inline key modifier, which means that the function is an inline function, and the compiler will try to do inline optimization, avoid the overhead of function calls, and improve the efficiency of code execution. 2) Applicable Scenarios--"small and simple functions that are called multiple times are suitable for inline--"The number of calls rarely gets large and complex functions that are not suitable for inline--the recursive function is not suitable for inline note: Inline is only a recommendation rather than a mandatory requirement, whether inline is primarily dependent on the compiler, some functions that do not add the inline keyword modification are also handled inline by default, and some functions are ignored by the compiler even if the inline keyword is added. ---------------------Written questions:1) Code Snippet 1 for(intI=0;i<100000; i++)    for(intj=0;j< -; j + +)      ... 2) Code Snippet 2 for(intI=0;i< -; i++)    for(intj=0;j<100000; j + +)      ...---------------------Nine C++The dynamic memory allocation//written questions: differences between malloc and free in C + + and New/delete1Review dynamic Assignment in C1Assignedmalloc()2Release Free()3) Error: return value eg:int* p = (int*)malloc(sizeof(int)); *p = -;  Free(p);//Avoid leaksp = NULL;//avoid using wild pointers2C++using operators to allocate dynamic memory1AssignedNew、New[]2ReleaseDelete、Delete[]3Error Handling: Exception (said later) eg://int* p = new int;//Assignment not initialized//*p = +;   int* p =New int( -);//simultaneous initialization of allocations   Deletep; P=NULL; -----------------int* Parr =New int[Ten];//Assigning Arraysparr[0]=Ten; parr[1]= -; ...   Delete[] Parr; Parr=NULL;--------------------------10 C++the Reference (Reference)1definition1a reference is an alias for a variable, and the action on the reference is exactly the same as the action on the variable. 2) syntax rule type& Reference name =variable name; Note: References must be initialized at the time of definition, and the target of the reference cannot be modified after initialization. Note: The reference type and the target variable type to which it is bound are consistent with eg:intA =Ten; int& B = A;//B is the alias of a.b++; cout<< a << Endl;// One intc = -; b= C;//assign the value of C to B (a)cout << a << Endl;// - 2frequently cited1When you define a reference, you add a const modifier, which is a constant reference, and you cannot modify the referenced target through a regular reference. ConstType & Reference name =variable name; eg:intA =Ten;Const int& B = A;//B is a constant reference to a.b = $;//Error2a normal reference can only refer to an lvalue, and a constant reference is also called a universal reference, which can reference either an lvalue or an rvalue. ==================Note: About left and right values1) Lvalue: Can be placed to the left of the assignment operator-Normal variables are left values--Front ++/--The expression result is a left-hand value-The result of an assignment expression is an lvalue eg:intA =1; ++a = -;//OKcout << a << Endl;// -++++++A; cout<< a << Endl;// ateg:intA =3, B =5; intc =0; (c= a) = B;//OKcout << c << Endl;//52Right value: can only be placed to the right of the assignment operator--"Literal constants--"The value of most expressions eg:intA =3, B =5; (A+ b) =Ten;//The result of error,a+b is the right value--"function return value eg:intFoovoid){      intA = -; returnA//Assigning temporary variables =a  }  intMainvoid)  {      intres = foo ();//function call result is temp variablecout << Res << Endl;// -      int& r = foo ();//Error      Const int& cr =foo (); return 0; }

Boolean type, operator alias, C + + function, dynamic memory allocation (NEW\DELETE), reference (DAY02)

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.