const笔记(不定期更新)

 1、如果要使用引用返回值,但又不允许给这个返回值赋值这样的操作,可以将返回值类型声明为const类型的引用。(成为不可修改的左值)
2、只读操作时,使用const&做参数,可以节省时间和内存。(相比于按值传递结构)
3、对于形参为const引用的C++函数,如果实参不匹配,则其行为类似于按值传递,为确保原始数据不被修改,将使用临时变量。
【意见保留】:如果已知void swap(int &a,int &b);
而long a=3,b=4;则:
swap(a,b);会给出warning,,,,但是在vs2012里亲测是error。
 如果看到了有关临时变量的警告,一定不要忽略。
4、常规变量和const变量都可以视为左值,因为可以通过地址访问他们,但常规变量属于可修改的左值,而const变量属于不可修改的左值。
5、【意见保留】:如果实参与引用参数不匹配,C++将生成临时变量。(感觉这可能是老式的标准了) 
6、 例题:
#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()
{
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);

display(accumulate(team,two));

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

dup=accumulate(team,five);
std::cout<<"Displaying team:\n";
display(team);

std::cout<<"Displaying dup after assignment:\n";
display(dup);

set_pc(four);

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;
}
7、 example: 
#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);

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 enhance:"<<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;
}

//程序试图返回局部变量或临时变量的地址(warning)
const string &version3(string &s1,const string &s2)
{
string temp;
temp=s2+s1+s2;

return temp;
}
【说明】:string类本身定义了一种char*到string的转换功能。形参为const string&时可以用的实参有,
                    string对象、C-风格字符串(用引号括起来的字符串字面量、有空字符结尾的char数组、指向
                    char的指针变量) 
8、 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值