继续我的《c++ Primer Plus 》的学习
- #include<iostream>
- using namespace std;
- struct sysop
- {
- char name[26];
- char quote[64];
- int used;
- };
- const sysop &use(sysop &sysopref);
- int main()
- {
- // 将引用应用于结构 /
- sysop looper =
- {
- "Rick /"Fortran/" Looper",
- "I'm a goto kind of guy.",
- 0
- };
- use(looper);
- 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";
- cout << endl << &looper <<" : " << ©cat << endl;
- return 0;
- }
- const sysop& use(sysop& sysopref) ///函数返回引用
- {
- cout << sysopref.name << " says:/n";
- cout << sysopref.quote << endl;
- sysopref.used++;
- return sysopref;
- }
- #include<iostream>
- #include<string>
- using namespace std;
- string version1(const string & s1, const string & s2);
- const string & version2(string & s1, const string & s2);
- const string & version3(string & s1, const string & s2);
- /// 下面这个程序是关于引用知识点的,理解这个程序的用法,引用基本上就OK了//
- int main()
- {
- 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;
- }
- string version1(const string & s1, const string & s2)
- {
- string temp;
- temp = s2 + s1 + s2;
- return temp;
- }
- const string & version2(string & s1, const string & s2)
- {
- s1 = s2 + s1 + s2;
- return s1;
- }
- const string & version3(string & s1, const string & s2) //这种返回局部变量的方法是错误的,编译时会有警告,在运行时会让系统崩溃
- {
- string temp;
- temp = s2 + s1 + s2;
- return temp;
- }