C++进阶--引用

目录

参考资料--《C++ Primer Plus 第六版》

1.引用变量

将 rodents 作为变量rats的别名;

这里的&不是地址运算符,而是类型标识符;(int &) 表示指向 int 的引用;

示例:

输出:

 其中&rodents 的 & 运算符是地址运算符,&rodents 表示 rodents 引用的变量地址;

2.将引用用作函数参数

函数传参通常分三种:

按值传递:

按指针传递:

按引用传递:

区别在于:

按引用传递,允许被调用的函数访问、改变调用函数中的变量本身;

按值传递,被调用函数改变的仅仅是调用函数中的变量的副本内容,而不是变量本身;

示例:

输出:

可以看到:

因为变量a,b是wallet1,wallet2的别名,所以交换了a和b相当于交换了wallet1和wallet2;

因为变量a,b是复制了wallet1,wallet2值的新变量,所以交换了a和b并不影响wallet1和wallet2的值;

因为指针p,q是指向wallet1,wallet2变量地址的指针,所以交换了(*p)和(*q)相当于交换了wallet1和wallet2;

const:

若仅仅让函数使用传递给它的信息,而并不对这些信息进行修改,同时又想使用引用,则应使用常量引用:

此时,若代码修改了a,b值时,编译器将报错:

若不许该a,b,但调用时参数不是 int 型,如double型:

此时编译器将创建两个临时int型变量用于函数计算,但函数返回后,a、b值仍然不变;

3.将引用用于结构

则可在函数中将指向该结构体的引用作为参数:

如果不希望函数修改传入的结构,则可以使用const:

示例:

程序说明:

程序首先//初始化多个结构对象;然后:

a.

因为 set_pc() 形参 ft 为引用,因此 ft 指向 one ,ft.percent = one.percent;

b.

因为 display() 显示结构内容,而不修改它,因此函数使用了const参数;

c.

函数值修改了第一个参数,所以第二个参数为const引用;

因为返回值为  free_throws & 引用,说明返回的是最初函数参数 team;

而若返回为 free_throws ,则仅仅是target(也就是team)的拷贝,与team本身无关;

返回引用不用经过拷贝到临时变量的操作,所以效率更高

返回值为 free_throws &:return  --  拷贝到临时变量  --  再赋值给接收返回值变量;

返回值为 free_throws:return  --  直接赋值给接收返回值变量;

因此,本程序以下两段代码等效:

同理,下面两段代码也等效:

d.

将team中的值复制到dup中;e.

4.将引用用于类对象

将类对象作为参数,并传递给函数,C++通常做法是使用引用;

示例:

输出:

程序说明:

1.

因为使用两个const参数,并且使用temp对象进行操作,所以函数执行完毕,输入参数(input)保持不变,输出为:

2.

第一个参数不是const,在程序中可直接修改原来的string对象,所以输入参数(input)会发生改变,输出为:

3.

与 version1()、version2()不同的是:

version1()的返回值是一个临时变量,函数执行完毕,临时变量temp的值将会被释放,temp会被复制到一个临时存储单元中,然后再main()中将该存储单元的内容赋值给string result对象;

version2()的返回值是输入参数引用 string & s1,函数执行完毕后,该值并不会被释放,所以返回值指向string & s1引用;

version3()的返回值temp是一个临时变量,函数执行完毕后该变量会被释放,而程序会试图引用一个被释放的变量,而不像version1()那样直接赋值临时存储单元的数据,所以执行该函数时会崩溃,输出为:


参考资料--《C++ Primer Plus 第六版》

1.引用变量

int & rodents = rats;

将 rodents 作为变量rats的别名;

这里的&不是地址运算符,而是类型标识符;(int &) 表示指向 int 的引用;

示例:

// firstref.cpp -- defining and using a reference
#include <iostream>
int main()
{
    using namespace std;
    int rats = 101;
    int & rodents = rats;   // rodents is a reference

    cout << "rats = " << rats;
    cout << ", rodents = " << rodents << endl;
    rodents++;
    cout << "rats = " << rats;
    cout << ", rodents = " << rodents << endl;

// some implementations require type casting the following
// addresses to type unsigned
    cout << "rats address = " << &rats;
    cout << ", rodents address = " << &rodents << endl;
    // cin.get();
    return 0; 
}

输出:

rats = 101, rodents = 101
rats = 102, rodents = 102
rats address = 0x61fe14, rodents address = 0x61fe14

 其中&rodents 的 & 运算符是地址运算符,&rodents 表示 rodents 引用的变量地址;

2.将引用用作函数参数

函数传参通常分三种:

按值传递:

swapv(int a, int b)

按指针传递:

swapp(int * p, int * q)

按引用传递:

swapr(int & a, int & b)

区别在于:

按引用传递,允许被调用的函数访问、改变调用函数中的变量本身;

按值传递,被调用函数改变的仅仅是调用函数中的变量的副本内容,而不是变量本身;

示例:

// swaps.cpp -- swapping with references and with pointers
#include <iostream>
void swapr(int & a, int & b);   // a, b are aliases for ints
void swapp(int * p, int * q);   // p, q are addresses of ints
void swapv(int a, int b);       // a, b are new variables
int main()
{
    using namespace std;
    int wallet1 = 300;
    int wallet2 = 350;

    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Using references to swap contents:\n";
    swapr(wallet1, wallet2);   // pass variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Using pointers to swap contents again:\n";
    swapp(&wallet1, &wallet2); // pass addresses of variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Trying to use passing by value:\n";
    swapv(wallet1, wallet2);   // pass values of variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;
    // cin.get();
    return 0; 
}

void swapr(int & a, int & b)    // use references
{
    int temp;

    temp = a;       // use a, b for values of variables
    a = b;
    b = temp;
}

void swapp(int * p, int * q)    // use pointers
{
    int temp;

    temp = *p;      // use *p, *q for values of variables
    *p = *q;
    *q = temp;
}

void swapv(int a, int b)        // try using values
{
    int temp;

    temp = a;      // use a, b for values of variables
    a = b;
    b = temp; 
}

输出:

wallet1 = $300 wallet2 = $350
Using references to swap contents:
wallet1 = $350 wallet2 = $300
Using pointers to swap contents again:
wallet1 = $300 wallet2 = $350
Trying to use passing by value:
wallet1 = $300 wallet2 = $350

可以看到:

swapr(wallet1, wallet2)

void swapr(int & a, int & b)
{...}

因为变量a,b是wallet1,wallet2的别名,所以交换了a和b相当于交换了wallet1和wallet2;

swapv(wallet1, wallet2)
void swapv(int a, int b)
{...}

因为变量a,b是复制了wallet1,wallet2值的新变量,所以交换了a和b并不影响wallet1和wallet2的值;

swapp(&wallet1, &wallet2);
void swapp(int * p, int * q)
{...}

因为指针p,q是指向wallet1,wallet2变量地址的指针,所以交换了(*p)和(*q)相当于交换了wallet1和wallet2;

const:

若仅仅让函数使用传递给它的信息,而并不对这些信息进行修改,同时又想使用引用,则应使用常量引用:

void swapr(const int & a, const int & b)
{...}

此时,若代码修改了a,b值时,编译器将报错:

error: assignment of read-only reference 'a'
error: assignment of read-only reference 'b'

若不许该a,b,但调用时参数不是 int 型,如double型:

double wallet1 = 300.0f
double wallet2 = 350.0f
swapr(wallet1, wallet2);
void swapr(const int & a, const int & b)
{...}

此时编译器将创建两个临时int型变量用于函数计算,但函数返回后,a、b值仍然不变;

3.将引用用于结构

struct free_throws
{
    std::string name;
    int made;
    int attempts;
    float percent;
};

则可在函数中将指向该结构体的引用作为参数:

void set_pc(free_throws & ft);

如果不希望函数修改传入的结构,则可以使用const:

void display(const free_throws & ft);

示例:

//strc_ref.cpp -- using structure references
#include <iostream>
#include <string>
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()
{
    //初始化多个结构对象
    //如果指定初始值比成员函数少,余下成员(percent)自动设置为零
    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};
    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);
    // std::cin.get();
    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;  
}

输出:

Name: Ifelsa Branch
  Made: 13      Attempts: 14    Percent: 92.8571
Name: Throwgoods
  Made: 13      Attempts: 14    Percent: 92.8571
Name: Throwgoods
  Made: 23      Attempts: 30    Percent: 76.6667
Name: Throwgoods
  Made: 35      Attempts: 48    Percent: 72.9167
Displaying team:
Name: Throwgoods
  Made: 41      Attempts: 62    Percent: 66.129
Displaying dup after assignment:
Name: Throwgoods
  Made: 41      Attempts: 62    Percent: 66.129
Displaying dup after ill-advised assignment:
Name: Whily Looper
  Made: 5       Attempts: 9     Percent: 55.5556

程序说明:

程序首先//初始化多个结构对象;然后:

a.

set_pc(one);
void set_pc(free_throws & ft)
{
    if (ft.attempts != 0)  
        ft.percent = 100.0f *float(ft.made)/float(ft.attempts); 
    else
        ft.percent = 0;
}

因为 set_pc() 形参 ft 为引用,因此 ft 指向 one ,ft.percent = one.percent;

b.

display(one); 
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';
}

因为 display() 显示结构内容,而不修改它,因此函数使用了const参数;

c.

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

函数值修改了第一个参数,所以第二个参数为const引用;

因为返回值为  free_throws & 引用,说明返回的是最初函数参数 team;

而若返回为 free_throws ,则仅仅是target(也就是team)的拷贝,与team本身无关;

返回引用不用经过拷贝到临时变量的操作,所以效率更高

返回值为 free_throws &:return  --  拷贝到临时变量  --  再赋值给接收返回值变量;

返回值为 free_throws:return  --  直接赋值给接收返回值变量;

因此,本程序以下两段代码等效:

accumulate(team, one);
display(team);
display(accumulate(team, two));

同理,下面两段代码也等效:

accumulate(accumulate(team, three), four);
accumulate(team, three);
accumulate(team, four);

d.

dup = accumulate(team,five);

将team中的值复制到dup中;
e.

 accumulate(dup,five) = four;

因为返回的是一个引用,所以这种赋值时允许的,等价于下面代码:

accumulate(dup,five);
dup = four;

4.将引用用于类对象

将类对象作为参数,并传递给函数,C++通常做法是使用引用;

示例:

// strquote.cpp  -- different designs
#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()
{
    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;
	// cin.get();
	// cin.get();
    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;
}

输出:

Enter a string: 1
Your string as entered: 1
Your string enhanced: ***1***
Your original string: 1
Your string enhanced: ###1###
Your original string: ###1###
Segmentation fault

程序说明:

1.

result = version1(input, "***");
string version1(const string & s1, const string & s2)
{
    string temp;

    temp = s2 + s1 + s2;
    return temp;
}

因为使用两个const参数,并且使用temp对象进行操作,所以函数执行完毕,输入参数(input)保持不变,输出为:

result = version1(input, "***");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;

//输出
Your string enhanced: ***1***
Your original string: 1

2.

result = version2(input, "###");
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对象,所以输入参数(input)会发生改变,输出为:

result = version2(input, "###");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;

//输出
Your string enhanced: ###1###
Your original string: ###1###

3.

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

与 version1()、version2()不同的是:

version1()的返回值是一个临时变量,函数执行完毕,临时变量temp的值将会被释放,temp会被复制到一个临时存储单元中,然后再main()中将该存储单元的内容赋值给string result对象;

version2()的返回值是输入参数引用 string & s1,函数执行完毕后,该值并不会被释放,所以返回值指向string & s1引用;

version3()的返回值temp是一个临时变量,函数执行完毕后该变量会被释放,而程序会试图引用一个被释放的变量,而不像version1()那样直接赋值临时存储单元的数据,所以执行该函数时会崩溃,输出为:

input = copy;
result = version3(input, "@@@");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;

//输出
Segmentation fault
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值