C++-Record09 -- return value of function as reference

Keywords: Programming

Catalog

Return stack variable

Return global variable

Return static variable

Overall code

Return stack variable

The following three functions are set to different return types. Return a value, a reference and a pointer respectively:

int getAA1()
{
	int a ;
	a = 10;
	return a;
}

//Return a itself return a copy of a 10 
int& getAA2()
{
	int a ; //If you return a reference on the stack, there may be a problem
	a = 10;
	return a;
}

int* getAA3()
{
	int a ;
	a = 10;
	return &a;
}

void main1101()
{
	int a1 = 0;
	int a2 = 0;
	a1 = getAA1();

	a2 = getAA2(); //10
	int &a3 = getAA2();  //If the return stack variable cannot be the initial value of other references

	printf("a1:%d \n", a1);
	printf("a2:%d \n", a2); 
	printf("a3:%d \n", a3);  // *a3

	cout<<"hello..."<<endl;
	system("pause");
	return ;
}

In debug mode, run:

In release mode, run:

a3 is not a mess!? Because different versions of the program run different results, this indicates that there are potential bug s in the program. Then, let's analyze:

Not to mention the memory space allocation of a1; directly speaking, a2 and a2 return the function return value by reference, and the essence of reference is that the compiler helps the programmer directly return the corresponding address, that is, directly return the address of a, and return the value of a (it can be interpreted as making a copy of the address and variable of a). After a runs, it is called The running space of a in the function does not need to be deconstructed, but it is returned by reference, which returns a reference of a, that is, the memory space of a; look at a3, which returns a reference, which is connected by a reference, and the compiler will put the return value (address of a) of getAA2(), which is equivalent to "oxaa11" in the variable a3, However, when we print a3, the compiler will execute "* a3" as soon as it sees that a3 is a reference. The compiler accesses the address of a3 and finds that the corresponding location has been cleared at the end of the run of the called function. Therefore, what is printed out is a garbled code.

Difficulties in using C + + References:

Conclusion: when the function return value is a reference, if the stack variable is returned, it cannot be the initial value of other references, it cannot be used as the left value, but only as the right value.

Return global variable

When the return value is a global variable:

//Variable is static or global

int j1()
{
	static int a = 10;
	a ++ ;
	return a;
}

int& j2()
{
	static int a = 10;
	a ++ ;
	return a;
}

//If static variable or global variable is returned
//	Can be the initial value of other references
//	It can be used as right value or left value
void main1112()
{
	int a1 = 10;
	int a2 = 20;

	a1 = j1();
	a2 = j2();
	int &a3 = j2();

	printf("a1:%d \n", a1);
	printf("a2:%d \n", a2); 
	printf("a3:%d \n", a3);  
	system("pause");
}

Different from the previous situation, the result can be run without random code.

Return static variable

Function when the left value is a static variable:

//---Function when left
//Returns the value of a variable
int g1()
{
	static int a = 10;
	a ++ ;
	return a;  //
} 

//Return variable itself is to return the memory space represented
int& g2()
{
	static int a = 10;
	a ++ ;
	printf("a:%d \n" , a);
	return a;
}

void main()
{
	// g1() = 100; the left operand must be the left value
	//11 = 100;

	g2() = 100; //The function return value is a reference, and when the left value 
	g2();

	int c1 = g1(); //Function return value is a reference, and when the right value 
	int c2 = g2(); //Function return value is a reference, and when the right value 
	//a = 100;
	system("pause");
}

The sentence "g2() = 100;" is equivalent to "a = 100", because G2 returns the variable itself, and the variable itself represents a memory space. Therefore, it can run successfully. This type of function return value is a reference value. The most important thing is whether the memory space is an effective memory space. In this example, "static int a = 10;" is an effective memory space, which will not be cleared with the structure of the called function.

 

Conclusion: if a static variable or a global variable is returned, it can be used as the initial value of other references, either as a right value or as a left value. In C + + chained programming, references are often used and operators overload topics.

 

Overall code

dm11 function return value is a reference.cpp

#include <iostream>
using namespace std;

int getAA1()
{
	int a ;
	a = 10;
	return a;
}

//Return a itself return a copy of a 10 
int& getAA2()
{
	int a ; //If you return a reference on the stack, there may be a problem
	a = 10;
	return a;
}

int* getAA3()
{
	int a ;
	a = 10;
	return &a;
}

void main1101()
{
	int a1 = 0;
	int a2 = 0;
	a1 = getAA1();

	a2 = getAA2(); //10
	int &a3 = getAA2();  //If the return stack variable cannot be the initial value of other references

	printf("a1:%d \n", a1);
	printf("a2:%d \n", a2); 
	printf("a3:%d \n", a3);  // *a3

	cout<<"hello..."<<endl;
	system("pause");
	return ;
}

//Variable is static or global

int j1()
{
	static int a = 10;
	a ++ ;
	return a;
}

int& j2()
{
	static int a = 10;
	a ++ ;
	return a;
}

//If static variable or global variable is returned
//	Can be the initial value of other references
//	It can be used as right value or left value
void main1112()
{
	int a1 = 10;
	int a2 = 20;

	a1 = j1();
	a2 = j2();
	int &a3 = j2();


	printf("a1:%d \n", a1);
	printf("a2:%d \n", a2); 
	printf("a3:%d \n", a3);  
	system("pause");
}


//////////////////////////////////////////////////////////////////////////
//---Function when left
//Returns the value of a variable
int g1()
{
	static int a = 10;
	a ++ ;
	return a;  //
} 

//Returns the variable itself, 
int& g2()
{
	static int a = 10;
	a ++ ;
	printf("a:%d \n" , a);
	return a;
}

void main()
{
	// g1() = 100;
	//11 = 100;

	g2() = 100; //The function return value is a reference, and when the left value 
	g2();

	int c1 = g1(); //Function return value is a reference, and when the right value 
	int c2 = g2(); //Function return value is a reference, and when the right value 
	//a = 100;
	system("pause");
}

 

Published 129 original articles, won praise 6, visited 4540
Private letter follow

Posted by flamtech on Sat, 18 Jan 2020 21:24:22 -0800