符号“&”和const在函数里不同位置的用法
2013-01-31 19:12
615 查看
[b]符号“&”和const在函数里不同位置的用法[/b]
其实引用的初衷是节省下空间,但是我们要注意引用是针对变量引入的。
1、在返回类型与函数名之间加“&”需要注意函数的返回值(能不加就最好不加)
(1)如果函数返回void则肯定可以用。
(2)如果函数返回一种既定类型的量则编译会有警告,因为函数调用完其被分配的栈空间就被释放了。
如 Team operator+(const Team & t) ; //2 用成员函数重载+运算符
函数体
Team Team::addtototal(const Team & t) { Team temp; temp.t_number = t_number + t.t_number; temp.t_totalscore = t_totalscore + t.t_totalscore; temp.averagescore(); return temp; } 加 & 号也可以运行,但会有如下警告: team.cpp: In member function ‘Team& Team::operator+(const Team&)’: team.cpp:73:7: warning: reference to local variable ‘temp’ returned 因为调用完,temp这个临时对象空间就被释放了,所以此处返回引用按理说时错误的。
友元重载同样会遇到上面情况:
friend Team operator+(const Team & t1,const Team & t2);//3 用友元函数重载+运算符
即temp的空间被释放掉了所以&temp没空间了
(3)若函数返回的事类中的默认this指针的值则会出现错误。
如Team operator>(const Team & t); //1 用成员函数重载>运算符
函数体
Team Team::operator>(const Team & t) { if(t.t_totalscore > t_totalscore) { return t; } else { return *this;//不用想太多,this就代表大的对象,由if的条件直接判断 } } 加 & 号不可运行,错误如下: team.cpp: In member function ‘Team& Team::operator>(const Team&)’: team.cpp:104:10: error: invalid initialization of reference of type ‘Team&’ from expression of type ‘const Team’
友元重载同样会遇到上面情况:
friend Team operator>(const Team & t1, const Team & t2); //2 用友元函数重载>运算符
即该对象用完到下一个对象时this就会立马改变了,所以返回会出错。
2、在函数参数的类型与变量间加&需要注意参数的类型(看实参是什么)
(1)若后面调用函数时给的实参是一个变量则编译不会出错。
Team::Team(int & t) { t_totalscore = t;
}
正确调用:
int m = 87; Team tm3(m);
Team::Team(int const t) { t_totalscore = t; }
正确调用
Team tm3(87);
(2)若后面调用函数是实参是整型常量、字符型常量则编译会出错,因为引用是变量的别名。
const的初衷是定义一些其内部值不可改变的数据,const用法只需明白其功能类比&即可得到
1、在函数返回类型之前的const
函数返回的值不可改变,即后面不能拿其返回值当变量用。
2、在形参类型之前的const(实参常数赋值后就不希望它改变)
函数的实参不能为变量。
Team::Team(int const t)
{
t_totalscore = t;
}
正确调用
Team tm3(87);
为变量时不能定义为const。
Team::Team(int & t)
{
t_totalscore = t;
}
正确调用:
int m = 87;
Team tm3(m);
3、类中在函数最后加的const(显思维)
执行函数时不改变类里的成员变量。
其实引用的初衷是节省下空间,但是我们要注意引用是针对变量引入的。
1、在返回类型与函数名之间加“&”需要注意函数的返回值(能不加就最好不加)
(1)如果函数返回void则肯定可以用。
(2)如果函数返回一种既定类型的量则编译会有警告,因为函数调用完其被分配的栈空间就被释放了。
如 Team operator+(const Team & t) ; //2 用成员函数重载+运算符
函数体
Team Team::addtototal(const Team & t) { Team temp; temp.t_number = t_number + t.t_number; temp.t_totalscore = t_totalscore + t.t_totalscore; temp.averagescore(); return temp; } 加 & 号也可以运行,但会有如下警告: team.cpp: In member function ‘Team& Team::operator+(const Team&)’: team.cpp:73:7: warning: reference to local variable ‘temp’ returned 因为调用完,temp这个临时对象空间就被释放了,所以此处返回引用按理说时错误的。
友元重载同样会遇到上面情况:
friend Team operator+(const Team & t1,const Team & t2);//3 用友元函数重载+运算符
即temp的空间被释放掉了所以&temp没空间了
(3)若函数返回的事类中的默认this指针的值则会出现错误。
如Team operator>(const Team & t); //1 用成员函数重载>运算符
函数体
Team Team::operator>(const Team & t) { if(t.t_totalscore > t_totalscore) { return t; } else { return *this;//不用想太多,this就代表大的对象,由if的条件直接判断 } } 加 & 号不可运行,错误如下: team.cpp: In member function ‘Team& Team::operator>(const Team&)’: team.cpp:104:10: error: invalid initialization of reference of type ‘Team&’ from expression of type ‘const Team’
友元重载同样会遇到上面情况:
friend Team operator>(const Team & t1, const Team & t2); //2 用友元函数重载>运算符
即该对象用完到下一个对象时this就会立马改变了,所以返回会出错。
2、在函数参数的类型与变量间加&需要注意参数的类型(看实参是什么)
(1)若后面调用函数时给的实参是一个变量则编译不会出错。
Team::Team(int & t) { t_totalscore = t;
}
正确调用:
int m = 87; Team tm3(m);
Team::Team(int const t) { t_totalscore = t; }
正确调用
Team tm3(87);
(2)若后面调用函数是实参是整型常量、字符型常量则编译会出错,因为引用是变量的别名。
const的初衷是定义一些其内部值不可改变的数据,const用法只需明白其功能类比&即可得到
1、在函数返回类型之前的const
函数返回的值不可改变,即后面不能拿其返回值当变量用。
2、在形参类型之前的const(实参常数赋值后就不希望它改变)
函数的实参不能为变量。
Team::Team(int const t)
{
t_totalscore = t;
}
正确调用
Team tm3(87);
为变量时不能定义为const。
Team::Team(int & t)
{
t_totalscore = t;
}
正确调用:
int m = 87;
Team tm3(m);
3、类中在函数最后加的const(显思维)
执行函数时不改变类里的成员变量。
相关文章推荐
- 类成员函数中的const不同位置的作用
- C++中const的不同位置的用法
- 函数参数中的const和&用法
- 虚函数用法和注意事项总结 .
- php strtotime函数和date函数用法
- # include <errno.h >查看错误代码errno是调试程序的一个重要方法。当Linux C API函数发生异常时,一般会将errno变量赋值一个整数,不同的值表示不同的含义,可以通过查看
- VC编译错误:error LNK2019: 无法解析的外部符号 "void __stdcall _com_issue_error(long)" (?_com_issue_error@@YGXJ@Z),该符号在函数 "void __cdecl _com_ut
- error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall Widget::metaObject
- Post 与 Get 用法不一样 "&"符号
- error LNK2019: 无法解析的外部符号 __imp__UuidToStringW@8,该符号在函数 "private: class ATL::CStringT<wchar_t,class S
- const的用法,特别是用在函数后面
- 【c++语言基础】在c++里&符号的用法
- EVC to VS2008 error LNK2019: 无法解析的外部符号 WinMain,该符号在函数 WinMainCRTStartup 中被引用 ENTRY:"WinMainCRTStartu
- C++学习笔记(3):const与&修饰函数形参的作用
- 函数fp()、数组a[i]和箭头符号->的由来
- Linux&C open creat read write lseek 函数用法总结
- 不同编译器 函数用法or 函数命名冲突用法(待完善)
- error LNK2019: 无法解析的外部符号 ___glutInitWithExit@12,该符号在函数 _glutInit_ATEXIT_HACK@8 中被引用 1>GEARS.obj : er
- php函数前面加&符号的意思是函数的引用
- shader之——球形烟雾 &&CG函数saturate与abs的用法