64,675
社区成员
发帖
与我相关
我的任务
分享
// C++ Primer.cpp
#include "stdafx.h"
#include <iostream>
class Example
{
private:
int m_val ;
public:
Example (int val): m_val(val) {}
// 此处的const限定的是什么?
const int & val (void)
{
return m_val ;
}
} ;
int main (void)
{
Example example(10) ;
int val = example.val() ;
// 我认为既然example.val()的返回类型为const int &, val引用这个返回值之后,就无法更改自己了.
// 事实上可以.为什么?
std ::cout << ++val << std ::endl ;
std ::cout << example.val() << std ::endl ;
std ::cin.get() ;
return 0 ;
}