const成员函数
来源:互联网 发布:淘宝文案岗位职责 编辑:程序博客网 时间:2024/06/06 01:41
现在getvalue()了const成员函数,这意味着我们可以称它为任何const对象。
const成员函数的类定义之外声明必须指定在函数原型中类的定义和代码文件中的函数原型const关键字:
重载函数与一个const和非const版本通常是当返回值需要不同的常量。在上面的例子中,对getvalue() const版本返回const引用,而非const版本返回一个非const引用。
让我们使我们的日期类的成员函数常量可以使用const对象看日期:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class
Date
{
private
:
int
m_nMonth;
int
m_nDay;
int
m_nYear;
Date() { }
// private default constructor
public
:
Date(
int
nMonth,
int
nDay,
int
nYear)
{
SetDate(nMonth, nDay, nYear);
}
void
SetDate(
int
nMonth,
int
nDay,
int
nYear)
{
m_nMonth = nMonth;
m_nDay = nDay;
m_nYear = nYear;
}
int
GetMonth() {
return
m_nMonth; }
int
GetDay() {
return
m_nDay; }
int
GetYear() {
return
m_nYear; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class
Date
{
private
:
int
m_nMonth;
int
m_nDay;
int
m_nYear;
Date() { }
// private default constructor
public
:
Date(
int
nMonth,
int
nDay,
int
nYear)
{
SetDate(nMonth, nDay, nYear);
}
void
SetDate(
int
nMonth,
int
nDay,
int
nYear)
{
m_nMonth = nMonth;
m_nDay = nDay;
m_nYear = nYear;
}
int
GetMonth()
const
{
return
m_nMonth; }
int
GetDay()
const
{
return
m_nDay; }
int
GetYear()
const
{
return
m_nYear; }
};
0 0
- const对象const成员函数
- const成员函数及const
- const成员函数
- const成员函数
- const 成员函数
- c++ const 成员函数
- 关于const成员函数
- const成员函数简介
- const 成员函数
- c++ const成员函数
- c++ const成员函数
- c++ const 成员函数
- c++const成员函数
- const成员函数
- const成员函数
- const成员函数
- c++ const成员函数
- const 成员函数
- 我的emacs配置
- 改变Static Text字体颜色
- 黑马程序员-线程范围内的数据共享之ThreadLocal
- 找不到方向往彩虹天堂
- matplot 简单使用
- const成员函数
- cocos2d-x中CCNode的init和onEnter
- 美国无线通信共同美分TeLexFREE多帐号批量贴广告助手程序下载
- RSA算法中利用欧几里得算法求d详细过程
- vfvfd
- 棋牌游戏带给人们的快乐
- [hdu 2018] 母牛的故事
- 黑马程序员-多个线程访问共享对象和数据
- VS2012C++中为组合框添加字符串问题