C++??????????const
???????????? ???????[ 2014/5/4 10:36:27 ] ????????C++ ???? net
?????μ???????????????????
???????c++??????????const?????????
????1.??????this?????const ?????????????
????2.const ???????????????????????????????
????(static??????????const???η???const ????????????this??????????????static????????)
????3.const ????????????????????????????
????4.const ?????????????÷?const ???????
????????????const??????const???????????????
????1.??const ?????????????????? const????
????2.const?????????????????????????ù????const????
????1-1
//error C2662: “Text::print”: ?????“this”????“const Text”????“Text &”
#include "stdafx.h"
#include "iostream"
using namespace std;
class Text{
public:
void printconst(void)const{cout<<"hello"<<endl;}
void print(void){cout<<"hello"<<endl;}
private:
int k;
};
//???漲??????Text???????????
int main(void)
{
a.printconst(); //ok
a.print(); //error
//????a.print()??????????
return 0;
}
|
//error C2272: “printconst”: ????????????????????η?
#include "stdafx.h"
#include "iostream"
using namespace std;
class Text{
public:
void static printconst(void) const{cout<<"hello"<<endl;}
void print(void){cout<<"hello"<<endl;}
private:
int k;
};
int main(void)
{
Text a;
a.printconst();
return 0;
}
|
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11