/* 7. Create two functions, one that takes a string* and one
that takes a string&. Each of these functions should
modify the outside string object in its own unique way.
In main( ), create and initialize a string object, print it,
then pass it to each of the two functions, printing the
results.
定义两个函数,一个获取string*,一个获取string&。每一个函数
以其特有的方式修改外部string对象。
在main()函数里,创建并定义一个string对象,打印出来,然后将
它传递给这两个函数,将结果打印出来。
*/
#include<iostream>
#include<string>
using namespace std;
void func1(string* a){
*a=*a+*a;
}
void func2(string& a){
a=a+a;
}
void main(){
string t="abc";
func1(&t);
cout<<t<<endl;
func2(t);
cout<<t<<endl;
}
that takes a string&. Each of these functions should
modify the outside string object in its own unique way.
In main( ), create and initialize a string object, print it,
then pass it to each of the two functions, printing the
results.
定义两个函数,一个获取string*,一个获取string&。每一个函数
以其特有的方式修改外部string对象。
在main()函数里,创建并定义一个string对象,打印出来,然后将
它传递给这两个函数,将结果打印出来。
*/
#include<iostream>
#include<string>
using namespace std;
void func1(string* a){
*a=*a+*a;
}
void func2(string& a){
a=a+a;
}
void main(){
string t="abc";
func1(&t);
cout<<t<<endl;
func2(t);
cout<<t<<endl;
}
本文介绍了一个C++程序示例,该程序定义了两个函数来修改字符串对象:一个通过指针,另一个通过引用。演示了如何使用这些函数来改变字符串的内容,并展示了在main函数中创建、初始化字符串对象的过程。
9111

被折叠的 条评论
为什么被折叠?



