????1-3

 

//error C2166: ?????? const ????
#include "stdafx.h"
#include "iostream"
using namespace std;
class Text{
public:
void printconst(void) const{cout<<"hello"<<endl; k=1;}
void print(void){cout<<"hello"<<endl;}
private:
int k;
};
int main(void)
{
Text a;
a.printconst();
return 0;
}

????????????????????

 

#include "stdafx.h"
#include "iostream"
using namespace std;
class Text{
public:
void printconst(void) const{k=1000;cout<<k<<endl;}
void print(void){cout<<"hello"<<endl;}
private:
static int k;
};int Text::k=500;
const Text a;
int main(void)
{
a.printconst();//ok
cin.get();
return 0;
}

?????????1000
????1-4

 

//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;print();}
void print(void){cout<<"hello"<<endl;}
private:
int k;
};
int main(void)
{
Text a;
a.printconst();
return 0;
}

????2-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;
};
const Text a;
int main(void)
{
a.printconst();//ok
a.print();//error
return 0;
}