引用,指针,值传递

2021-04-13 16:14:30 浏览数 (1)

1.引用的好处

代码语言:javascript复制
#include
using namespace std;
void swapr( int a, int b);
void swapp(int &a, int &b);
void swapv( int* a, int* b);
int main()
{
	int wallet1 = 100, wallet2 = 50;
	cout << "wallet1=" << wallet1;
	cout << "wallet2=" << wallet2 << endl;
	swapr(wallet1, wallet2);
	cout << "wallet1=" << wallet1;
	cout << "wallet2=" << wallet2 << endl;
	swapp(wallet1, wallet2);
	cout << "wallet1=" << wallet1;
	cout << "wallet2=" << wallet2 << endl;
	swapv(&wallet1, &wallet2);
	cout << "wallet1=" << wallet1;
	cout << "wallet2=" << wallet2 << endl;
	return 0;
}
void swapr(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
}
void swapp(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}
void swapv( int* a, int* b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

其中运用了指针,引用进行数互换,但是它们所执行完的效果却是不一样的。 通过观察发现,引用传递和按值传递的形式类似,只能从它对于函数的定义里才能看出区别。 引用和指针成功交换了wallet里的内容,值传递却没能完成这个任务。在引用传递中,a,b是wallet1,wallet2的别名,值传递,a,b是复制了wallet1和wallet2里面的内容,因此交换a,b就不会影响实参的值。

2.尽可能使用const,引用非常适用于结构和类

代码语言:javascript复制
#include 
#include 
struct free_throws
{
    std::string name;
    int made;
    int attempts;
    float percent;
};

void display(const free_throws& ft);
void set_pc(free_throws& ft);
free_throws& accumulate(free_throws& target, const free_throws& source);

int main()
{
    // partial initializations – remaining members set to 0
    free_throws one = { "Ifelsa Branch", 13, 14 };
    free_throws two = { "Andor Knott", 10, 16 };
    free_throws three = { "Minnie Max", 7, 9 };
    free_throws four = { "Whily Looper", 5, 9 };
    free_throws five = { "Long Long", 6, 14 };
    free_throws team = { "Throwgoods", 0, 0 };
    // no initialization
    free_throws dup;

    set_pc(one);
    display(one);
    accumulate(team, one);
    display(team);
    // use return value as argument
    display(accumulate(team, two));
    accumulate(accumulate(team, three), four);
    display(team);
    // use return value in assignment
    dup = accumulate(team, five);
    std::cout << "Displaying team:n";
    display(team);
    std::cout << "Displaying dup after assignment:n";
    display(dup);
    set_pc(four);
    // ill-advised assignment
    accumulate(dup, five) = four;
    std::cout << "Displaying dup after ill-advised assignment:n";
    display(dup);
    return 0;
}

void display(const free_throws& ft)
{
    using std::cout;
    cout << "Name: " << ft.name << 'n';
    cout << " Made: " << ft.made << 't';
    cout << "Attempts: " << ft.attempts << 't';
    cout << "Percent: " << ft.percent << 'n';
}
void set_pc(free_throws& ft)
{
    if (ft.attempts != 0)
        ft.percent = 100.0f * float(ft.made) / float(ft.attempts);
    else
        ft.percent = 0;
}

free_throws& accumulate(free_throws& target, const free_throws& source)
{
    target.attempts  = source.attempts;
    target.made  = source.made;
    set_pc(target);
    return target;
}

此代码运用了大量的引用 我们会发现最后的accumulate()函数比较奇特,它是返回了结构的引用,如果它是返回一个结构,将把结构复制到一个临时位置,再将这个拷贝复制给dup,但在返回值引用的时候,直接把team复制到dup,它的效率更高。

3.程序崩溃

代码语言:javascript复制
#include 
#include 
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()
{
    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) // has side effect
{
    s1 = s2   s1   s2;
    // safe to return reference passed to function
    return s1;
}

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;
}

temp是一个新的string对象,执行完函数,它将不复存在,因此返回指向temp的引用不可行。这也是第三个函数崩溃的原因。

0 人点赞