[C + + tutorial 07] reference

[C + + tutorial 07] reference

reference resources

  • Fan Lei C + + (session 9)
  • Xcode VS2015

content

What is reference

Referenced address

#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
    int a;
    int &ra=a;//Reference needs to be initialized
    cout<<"a:\t"<<&a<<endl;
    cout<<"ra:\t"<<&ra<<endl;
    return 0;
}

Output results:

A reference is an alias variable

#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
    int a;
    int &ra=a;//Reference needs to be initialized
    a=999;
    cout<<"&a:\t"<<&a<<endl;
    cout<<"&ra:\t"<<&ra<<endl;
    int b=888;
    ra=b;
    cout<<"&a:\t"<<&a<<endl;
    cout<<"&ra:\t"<<&ra<<endl;
    cout<<"&b:\t"<<&b<<endl;
    cout<<"a:\t"<<a<<endl;
    cout<<"ra:\t"<<ra<<endl;
    cout<<"b:\t"<<b<<endl;
    ra=1;
    cout<<"a:\t"<<a<<endl;
    cout<<"ra:\t"<<ra<<endl;
    cout<<"b:\t"<<b<<endl;
    //&RA = B; / / illegal operation
    return 0;
}

Output results:

Reference object

#include <iostream>
using namespace std;
class Human
{
public:
    int get()const{return i;}
    void set(int x){i=x;}
private:
    int i;
};

int main(int argc, const char * argv[]) {
    Human Mike;
    Human &rMike=Mike;
    rMike.set(123);
    cout<<rMike.get()<<endl;
}

Output results:

Empty reference

Pass by value

by address

Pass by alias

Pointer error rate is high and hard to read.

Using pointers and references to return multiple values

Examples are omitted.

Transfer objects by value

Insert a code slice here

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

class A
{
public:
	A() { cout << "Execute constructor to create an object" << endl; }
	A(A&) { cout << "Execute the copy constructor to create a copy of the object" << endl; }//Constructor with parameters
	~A() { cout << "Execute destructor to delete the object"<< endl; }
};
A func(A one) //The return value is an object of class A, and the input value is an object of class A.
{
	return one;
}
int main(int argc, const char * argv[]) {
	A a;
	func(a);//Pass by value
	system("pause");
	return 0;
}

Output results:


Note the meaning of the assignment constructor!!

Deliver objects by address

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

class A
{
public:
	A() { cout << "Execute constructor to create an object" << endl; }
	A(A&) { cout << "Execute the assignment constructor to create a copy of the object" << endl; }//Constructor with parameters
	~A() { cout << "Execute destructor to delete the object"<< endl; }
};
A* func(A *one) //The return value is an object of class A, and the input value is an object of class A.
{
	//return *one; / / return according to the value. The assignment constructor will also be called
	return one;//Return an address
}
int main(int argc, const char * argv[]) {
	A a;
	func(&a);//by address 
	system("pause");
	return 0;
}

Output results:

Use const pointer to pass object




But using const can be cumbersome

Passing objects by alias

The complexity of using const is omitted.

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
class A
{
public:
	A() { cout << "Execute constructor to create an object" << endl; }
	A(A&) { cout << "Execute the copy constructor to create a copy of the object" << endl; }//Constructor with parameters
	~A() { cout << "Execute destructor to delete the object"<< endl; }
	void set(int i) { x = i; }
	int get() const { return x; }
private:
	int x;
};
const A& func(A &one) 
{
	return one;
}
int main(int argc, const char * argv[]) {
	A a;
	a.set(11);
	const A &b = func(a);//So you can't change a by alias
	//A b = func(a); / / cannot assign a reference to an object
	cout << b.get() << endl;
	system("pause");
	return 0;
}

Output results:


Reference implements the same effect as pointer and does not use const, so it is more convenient to operate.

Pointer or reference

  • References can only be initialized, but pointers can be assigned;
  • The pointer can be null but the reference must be initialized;
  • The memory in the heap must be accessed by a pointer. When applying for the heap, the pointer is returned.
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
	int *p = new int;
	if (p != NULL)
	{
		int &r = *p;//Initializes r as an alias for the data in the memory space that p points to.
		r = 3;
		cout << r << endl;
	}
	//r = 4; / / and the lifetime of r is only valid within the braces.
	system("pause");
	return 0;
}

Quote easy mistakes

The last few hours need to study hard!

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
class A
{
public:
	A(int i) { cout << "Execute constructor to create an object"<< endl; x = i; }
	A(A&a) { x = a.x; cout << "Execute copy constructor to create an object"<< endl; }//copy constructor 
	~A(){ cout << "Execute destructor" << endl; }
	int get()const { return x; }
private:
	int x;
};
A func()//Returns the alias of the created object a. / / when passed by value, the value of the replica is also returned. Its lifetime is greater than that of its reference. //No problem with the pointer
{
	cout << "Jump to func In function" << endl;
	A a(23);// A ends its life after the braces, a disappears, and returns the alias of an object that does not exist.
	cout << "object a Address:"<<&a << endl;
	return a;
}

int main(int argc, const char * argv[]) {
	A *r = &func();//Aliases for nonexistent objects
	cout << "object a Address of copy of:" << r << endl;
	cout << r->get() << endl;
	system("pause");
	return 0;
}

See courseware for specific analysis

Tags: Session xcode

Posted on Mon, 15 Jun 2020 03:33:02 -0400 by ianwest