标签:
??? ? ?
问题:
? ?
#include "stdafx.h"
#include <iostream>
using namespace std;
?? ? ?
class Time
{
public:
Time():year(2015)
{
?}
? ?
?void show_time (void) const
{
cout<<"year:"<<year<<endl;
}
? ? ?
void print (int i)
{
cout<<"fun i:"<<i<<endl;
}
? ? ?
void print ( int i) const
{
cout<<"const fun i:"<<i<<endl;
}
? ? ?
private:
const int year;
};
? ?? ? ?
int _tmain(int argc, _TCHAR* argv[])
{
Time time;
time.show_time();
time.print(1);
? ? ?
Time const ctime;
ctime.show_time();
ctime.print(1);
? ? ?
system("pause");
return 0;
}
? ? ?
/*
year:2015
year:2015
请按任意键继续. . .
*/
class Time
{
public:
void show_time (void) const
{
cout<<"year:"<<year<<endl;
year = 11;//error C3490: 由于正在通过常量对象访问"year",因此无法对其进行修改
}
? ? ?
? ? ?
private:
int year;
};
? ?
class Time
{
public:
????Time():year(2015)
????{
???????? ?
????}
? ?
????void show_time (void) const
????{
????????cout<<"year:"<<year<<endl;
????????print(11);//error C2662: "Time::print": 不能将"this"指针从"const Time"转换为"Time &"
????}
? ?
????void print (int i)
????{
????????cout<<"fun i:"<<i<<endl;
????}
? ?
private:
????int year;
};
? ?
class Time
{
…
void show_time (void) const
{
cout<<"year:"<<year<<endl;
}
…
? ?
private:
const int year;
};
? ?
??
标签:
原文地址:http://www.cnblogs.com/mylinux/p/4728372.html