Indispensable Windows Native-C + +: reference types

Source: Internet
Author: User

[SOURCE DOWNLOAD]


Indispensable Windows Native-C + +: reference types



Webabcd


Introduced
Essential C + + of Windows Native

    • Reference type



Example
CppReference.h

#pragma<string>usingnamespace  std; namespace nativedll{    class  cppreference    {    public:         string  Demo ();    };}


CppReference.cpp

/** Reference type * * Reference can also be called "Alias" * Note: * 1, when declaring a reference, you must initialize * 2, the referenced object must have been allocated space * 3, the referenced object cannot be an address, that is, pointer variables, array variables, etc. cannot be referenced*/#include"pch.h"#include"CppReference.h" using namespaceNativedll;voidreference_demo1 ();voidReference_demo2 ();voidReference_demo3 ();stringcppreference::D emo () {//usage of referencesReference_demo1 (); //the difference between a reference and a pointerReference_demo2 (); //"Reference" can also be used as the return value of a functionReference_demo3 (); return "look at the code and the comments.";}//usage of referencesvoidReference_demo1 () {intA1, a2 =Ten; //&b-Represents the definition of a reference named B. The "&" here is a type specifier, indicating that B is a reference//declares a reference, it must be initialized at the same time    int&b = A1;//B is a reference to A1, that is, B is the alias of A1b= A2;//A1 and B are equal to tenA1 = -;//A1 and B are equal tob = +;//A1 and B are equal to}//the difference between a reference and a pointervoidReference_demo2 () {intm =1; intn =2; int*x = &m; int*y = &N; int&s =m; int&t =N; voidMy_swap (int*i,int*J);//swap two integers with pointers    voidMy_swap (int&i,int&AMP;J);//Exchange two integers by referenceMy_swap (x, y);//call void My_swap (int *i, int *j); Result: M=2,n=1My_swap (S, t);//call void My_swap (int &i, int &j); Result: m=1,n=2My_swap (M, n);//call void My_swap (int &i, int &j); Result: M=2,n=1}//swap two integers with pointersvoidMy_swap (int(Inint*j) {    //The formal parameter is a copy of the argument, where the pointer is copied and released immediately after the function call ends    inttemp; Temp= *i; *i = *J; *j =temp;}//Exchange two integers by referencevoidMy_swap (int&i,int&j) {    //In the case of "references", I and J are actually the corresponding two arguments themselves .    inttemp; Temp=i; I=J; J=temp;}//"Reference" can also be used as the return value of a functionint&reference_function ();intReference_i =0;voidReference_demo3 () {reference_function ()=999; //at this point the value of reference_i is 999}int&reference_function () {returnreference_i;}



Ok
[SOURCE DOWNLOAD]

Indispensable Windows Native-C + +: reference types

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.