C + + references, type conversions, classes, and objects (DAY03)

Source: Internet
Author: User
Tags define function

Ten C + +the Reference (Reference)1 2 3Reference function Parameters1You can modify the value of an argument variable and also reduce the cost of a function call by referencing the arguments to the function. 2A reference parameter may accidentally modify the value of an argument, and if you do not want to modify the real parametric itself, you can define it as a constant reference, increasing the efficiency of the pass and also receiving the constant type arguments. 4reference function return value1You can declare a function return type as a reference, avoiding the overhead of a function return value. 2a function return type is declared as a reference, then the function return value can be an lvalue. 3to avoid modifying the target of a reference outside the function, you can attach a constant property to the reference. Eg:int& Foo (void){     Static intA = -; returnA; }  intMainvoid) {foo ()= $;//OKNote: Do not return a reference to a local variable because the referenced target memory is released after the function returns, dangerous! However, you can return a reference to a member variable, a static variable, a global variable//written question: The difference between a reference and a pointer ...5References and Pointers1From the C language perspective, the nature of the reference is the pointer, but in C + +It is recommended to use references instead of pointers. Eg:DoubleD =3.14; Double& rd =D; Double*ConstPD = &D; Rd<= equivalent =>*PD2The pointer definition may not be initialized, its target can be modified (except for pointer constants), and references must be initialized, and once initialized the referenced target can be changed again. Eg:intA=3, b=5; int* p;//OK, can not initialize//int& R;//Errorp = &A; P= &b; int& r =A; R= b;//instead of modifying the target of a reference, you assign a value to R (a)//later understand3you can define pointers to pointers (level two pointers), but you cannot define referenced pointers eg:intA = -; int* p = &A; int* * pp = &p; int& r =A; int& * PR = &r;//Error  int* PR = &r;//OK, it's a normal pointer.4You can define a reference to a pointer, but you cannot define a referenced reference eg:intA = -; int* p = &A; int* & rp = P;//OK, reference to the pointer-----------int& r =A; int&& rr = r;//error, called Rvalue reference in C++11  int& r2 = R;//OK, not referred to as a reference, but a normal reference, equivalent to a again an alias.   5) can be an array of pointers, but cannot reference an array eg:intA=1, b=2, c=3; int* parr[3] = {&a,&b,&c};//Array of pointers  int& rarr[3] = {A,b,c};//Error6) You can define an array reference eg:intarr[3] = {1,2,3}; //Rarr is called an array reference, and an alias is given to the array  int(&rarr) [3] = arr;//OKarr[0] <-equivalence---rarr[0]7As with function pointers, you can define function references, and the syntax and function pointers are consistent. Eg:voidFuncintAintb)  {...} intMainvoid){       //defining and using function pointers       void(*pfunc) (int,int) =func; Pfunc (Ten, -); //defining and using function references       void(&rfunc) (int,int) =func; Rfunc (Ten, -); }======================================11 Type Conversions1implicitly-typed conversions eg:Charc ='A'; intn = C;//Implicit type conversions-----------voidFoointN)  {..} Foo (c);//Implicit type conversions-----------intFoovoid){        Charc ='A'; returnC//Implicit type conversions  }2force type conversions eg:Charc ='A'; intn = (intC//C-style forced conversions  intn =int(c);//C + +-style casts3C++An explicit type conversion of four operator forms is added1) static type conversion syntax: Target type variable=static_cast< target type >(Source type variable), applicable scenario: for the void*convert to a different type of pointer. Eg:intA = -; void* PV = &a;//OK    int* pi = PV;//Error    int* pi = static_cast<int*> (PV);//OK   2) dynamic type conversion (later) syntax: target type variable=dynamic_cast< target type >(source type variable);3) constant type conversion syntax: Target type variable=const_cast< target type >(source type variable); applicable scenario: Used to remove a pointer or reference to a constant attribute eg:intA = -; Const int* PA = &A; *PA = $;//Error   int* PA2 = const_cast<int*>(PA); *PA2 = $;//OK-------------------Const int& r =A; R= -;//Error   int& r2 = const_cast<int&>(R); R2= -;//OK   4) to re-interpret the type conversion syntax: Target type variable=reinterpret_cast< target type >(source type variable); applicable scenario:-conversions between arbitrary types of pointers or references-conversions between pointers and integers eg:intAddr =0x12345678; int* p =reinterpret_cast<int*> (0x12345678); *p = -;======================Summary:1Use macros sparingly, with const,enum, inline replacement#definePai 3.14--"Const double PAI = 3.14#defineState_sleep 0#defineState_run 1#defineState_stop 2--"enumState{sleep,run,stop};#defineMax (a) (a) > (b)? ( A):(B))--inlineintMaxintAintb) {        returna > B?a:b; }2variable is initialized with declaration at the same time3Use New/delete as far as possible to replace malloc/ Free4Use less void*, pointer calculations, unions, casts5Try to use string to represent the string, less char* in Cthe string represented by====================================12 Classes and Objects//Understand1What is an object? All things are objects, and any kind of things can be regarded as objects. 2How do I describe an object? The object is described and expressed by its attributes (nouns, quantity words, adjectives) and behavior (verbs). 3Object-oriented programming is an idea and method of introducing object observation into programming practice in the natural world. This method is called"Data Abstraction", that is, when describing the object, the details are stripped out, only the general, the regularity and the unity of things are considered. 4What is a class?A class is a new type of data that is extracted from the generality of multiple objects and is an abstract description of the properties and behavior of the object. 

C + + references, type conversions, classes, and objects (DAY03)

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.