1. 程式人生 > >C++ 字串 15-- 18.41.結構體與string string類的呼叫 引數通過引用的方式呼叫

C++ 字串 15-- 18.41.結構體與string string類的呼叫 引數通過引用的方式呼叫

#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
18.41.結構體與string string類的呼叫 引數通過引用的方式呼叫
---------------------------------*/
string show1(string str)
{
	cout<<str<<endl;
	return str;
}
string& show2(string &str) //引數通過引用的方式呼叫
{
	cout<<str<<endl;
	return str;
}
int main()
{
	string str1="hello world";
	string str2=show1(str1);
	cout<<str2<<endl;
	cout<<"------------"<<endl;
	str2=show2(str1); //引數通過引用的方式呼叫
	cout<<str2<<endl;
	return 0;
}

執行結果:

hello world
hello world
------------
hello world
hello world
Press any key to continue