第一個(gè)程序:
#include "stdafx.h"
#include <iostream>
using namespace std;
struct sysop
  {
char name[26];
char quote[64];
int used;
};
const sysop & use(sysop & sysopref);//function with a reference return type
int main(int argc, char* argv[])
  {
// NOTE: some implementations require using the keyword static
// int the two structure declarations to enable initialization
sysop looper=
 {
"Rick \"Fortran\" Looper",
"I'm a goto kind of guy.", //記住無遺漏逗號(hào)
0
};
use(looper); //looper is type sysop
cout<<"Looper: "<<looper.used<<" use(s)\n";
sysop copycat;
copycat = use(looper);
cout<<"Looper: "<<looper.used<<" use(s)\n";
cout<<"Copycat: "<<copycat.used<<" use(s)\n";
cout<<"use(looper): "<<use(looper).used<<" use(s)\n";
return 0;
}

const sysop & use(sysop & sysopref)
  {
cout<<sysopref.name<<" says:\n";
cout<<sysopref.quote<<endl;
sysopref.used++;
return sysopref;
// 通常,返回機(jī)制將返回值復(fù)制到臨時(shí)存儲(chǔ)區(qū)域中,隨后調(diào)用程序?qū)⒃L問該區(qū)域。
// 然而,返回引用意味著調(diào)用程序?qū)⒅苯釉L問返回值,而不需要拷貝。通常,引
// 用將指向傳遞給函數(shù)的引用,因此調(diào)用函數(shù)實(shí)際上是直接訪問自己的一個(gè)變量。
}
第二個(gè)程序:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string version1(const string & s1,const string & s2);
const string & version2(string & s1, const string & s2); //has side effect
const string & version3(string & s1, const string & s2); //bad design
int main(int argc, char* argv[])
  {
string input;
string copy;
string result;

cout<<"Enter a string: ";
getline(cin, input);
copy = input;
cout<<"Your string as entered: "<<input<<endl;
result = version1(input, "***");
cout<<"Your string enhanced: "<<result<<endl;
cout<<"Your original string: "<<input<<endl;

result=version2(input,"###");
cout<<"Your string enhanced: "<<result<<endl;
cout<<"Your original string: "<<input<<endl;
cout<<"Resetting original string.\n";
input = copy;
result = version3(input, "@@@");
cout<<"Your string enhanced: "<<result<<endl;
cout<<"Your original string: "<<input<<endl;
return 0;
}

 /**//* 接受兩個(gè)string參數(shù),并使用string類的相加功能來創(chuàng)建滿足要求的新字符串。
* 這兩個(gè)參數(shù)都是const引用。如果使用string對(duì)象作為參數(shù),最終結(jié)果將不變。
* 在這種情況下,s1和s2將為string對(duì)象。使用引用的效率更高,因?yàn)楹瘮?shù)不需
* 要?jiǎng)?chuàng)建新的string對(duì)象,并將原來對(duì)象中的數(shù)據(jù)復(fù)制到新對(duì)象中。
*
* temp是一個(gè)新的string對(duì)象,只在函數(shù)version1()中有效,該函數(shù)執(zhí)行完畢后,
* 它將不再存在。因此,將返回指向temp的引用不可行,因此該函數(shù)的返回類型
* 是string,這意味著temp的內(nèi)容將被復(fù)制到一個(gè)臨時(shí)返回存儲(chǔ)單元中。然后在
* main()中,該返回存儲(chǔ)單元的內(nèi)容將被復(fù)制到一個(gè)名為result的string中。
*/
string version1(const string & s1, const string & s2)
  {
 /**//* 讀者可能注意到一點(diǎn),"***"是const char *,而形參s2是const string &
* 這是因?yàn)椋谝唬瑂tring類定義了一種char *到string得轉(zhuǎn)換功能,這使得
* 可以使用C-style string來初始化string對(duì)象,第二,前面討論的類型為const
* 引用的形參的一個(gè)屬性。假設(shè)實(shí)參的類型與引用參數(shù)類型不匹配,但可被轉(zhuǎn)換
* 為引用類型,程序?qū)?chuàng)建一個(gè)正確類型的臨時(shí)變量,使用轉(zhuǎn)換后的實(shí)參值來初
* 始化它,然后傳遞一個(gè)指向該臨時(shí)變量的引用。這種屬性的結(jié)果是,如果形參
* 類型為const string &,在調(diào)用函數(shù)時(shí),使用的實(shí)參可以是string對(duì)象或C-style
* string,如用引號(hào)括起的字符串字面量、以空字符結(jié)尾的char數(shù)組或指向char的
* 指針變量
*/
string temp;
temp=s2+s1+s2;
return temp;
}


 /**//* version2()不能創(chuàng)建臨時(shí)string對(duì)象,而是直接修改原來的string對(duì)象
* 該函數(shù)可以修改s1,因?yàn)椴煌趕2,s1沒有被聲明為const。
*/
const string & version2(string & s1, const string & s2)
  {
s1=s2+s1+s2;
// safe to return reference passed to function
return s1;
}

 /**//* 存在一個(gè)致命的缺陷:返回一個(gè)指向version3()中聲明的變量的引用。這個(gè)函數(shù)
* 能夠通過編譯(但編譯器會(huì)發(fā)出警告),但當(dāng)程序視圖執(zhí)行該函數(shù)時(shí)將崩潰。是
* 因?yàn)槌绦蛞晥D引用已經(jīng)釋放的內(nèi)存。
*/
const string & version3(string & s1, const string &s2)//bad design
  {
string temp;
temp=s2+s1+s2;
// unsafe to return reference to local variable
return temp;
}
|