class Time{
public:
Time(int=0,int=0,int=0);
void call();
int hour;
int min;
int sec;
};
Time::Time(int h,int m,int s){
hour=h;
min=m;
sec=s;
}
Time const t(2,3,4);//t爲常對象
|
class Time{
public:
…
void call() const;
…
};
void Time::call() const{
cout<<"Call:"<<hour<<"-"<<min<<"-"<<sec<<endl;
}
…
Time const t(2,3,4);
t.call();
|
class Time{
public:
…
void setHour(int) const;
mutable int hour;//可以修改hour的值,通過setHour()
int min; //不可以修改min的值,read-only
int sec; //不可以修改sec的值,read-only
…
};
void Time::setHour(int h) const{
hour=h;
}
…
Time const t(2,3,4);
t.setHour(5);
|
class Time{
public:
Time(int,int,int);
const int hour;//聲明hour爲常數據成員
int min;
int sec;
};
Time::Time(int h,int m,int s):hour(h){//通過參數初始化表hour(h)對常數據成員hour初始化
min=m;
sec=s;
}
…
Time t(2,3,4);
|
const int hour;
int min;
int sec;
…
Time::Time(int h,int m,int s){ error:uninitialized member 'Time::hour' with 'const' type 'const int'
hour=h;
min=m;
sec=s;
}
|
class Time{
public:
…
void call() const; //聲明常成員函數
…
};
void Time::call() const{//定義常成員函數
cout<<"Call:"<<hour<<"-"<<min<<"-"<<sec<<endl;
}
|
數據成員
|
非const成員函數
|
const成員函數
|
非const數據成員
|
get/set
|
get/NO set
|
const數據成員
|
get/NO set
|
get/NO set
|
const對象的數據成員
|
NO get/NO set
|
get/NO set
|
Time t(2,3,4);
Time *const p=&t;//聲明常指針p並使其指向對象t
|
Time t(2,3,4);
Time *const p;error:uninitialized const 'p'
p=&t;
|
Time t(2,3,4);
Time *const p=&t;
Time tt(1,1,1);
p=&tt;error:assignment of read-only variable 'p'
|
class Time{
public:
…
void call();
int hour;
private:
int min;
…
void Time::call() {
cout<<"Call:"<<hour<<"-"<<min<<"-"<<sec<<endl;
}
…
int main(){
Time t(2,3,4);
Time *const p=&t;
p->call();
p->hour=0;
//p->min=0; error:`int Time::min' is private
p->call();
…
|
const char *ptr;
|
const char c[]="china";
const char *p1; //指向常變量的指針變量
p1=c;//ok
char * p2;//一般的指針變量
p2=c;error: invalid conversion from 'const char*' to 'char*'
|
char c[]="china";
const char *ptr;
ptr=c;//ok
*ptr='C';//將首字母改爲大寫error:assignment of read-only location
|
c[0]='C';
|
int main(){
void fun(int *); //函數聲明
int num[]={1,2,3,4};
int *p1;//定義一個一般的指針變量
const int *p2;//定義一個指向整型常變量的指針變量
p1=num;
p2=num;
fun(p1);//ok
fun(p2); error:invalid conversion from 'const int*' to 'int*'
}
void fun(int *p){//定義一個函數,它的形參是一般的指針
*p='0'; //改變所指向的變量的值
}
|
void fun(const int *p){//定義一個函數,它的形參是指向const變量的指針
*p='0'; error:assignment of read-only location
}
|
void fun(const int *); //函數聲明
int num[]={1,2,3,4};
int *p1;//定義一個一般的指針變量
const int *p2;//定義一個指向整型常變量的指針變量
p1=num;
p2=num;
fun(p1);//ok
fun(p2); //ok
|
形參
|
實參
|
合法否
|
改變指針變量所指向的變量的值
|
指向非const型變量的指針
|
非const變量的地址
|
合法
|
可以
|
指向非const型變量的指針
|
const變量的地址
|
非法
|
/
|
指向const型變量的指針
|
非const變量的地址
|
合法
|
不可以
|
指向const型變量的指針
|
const變量的地址
|
合法
|
不可以
|
Time const t(2,3,4);
const Time *p1;//定義一個指向常對象的指針變量
Time *p2;// 定義一個一般的指針變量
p1=&t;//ok
p2=&t;error:invalid conversion from `const Time*' to `Time*'
|
Time t(2,3,4);
const Time *p=&t;
cout<<p->hour<<endl;//ok
p->hour=0;error:assignment of data-member 'Time::hour' in read-only structure
|
void fun(const Time *p){//定義一個函數,它的形參是指向const對象的指針
p->hour=11;error: assignment of data-member 'Time::hour' in read-only structure
cout<<p->hour<<endl;
}
int main(){
void fun(const Time *); //形參是指向常對象的指針變量
Time t(2,3,4);
fun(&t);
…
|
void fun(Time *p){
p->hour=11;//ok
cout<<p->hour<<endl;
}
int main(){
void fun(Time *);
Time const t(2,3,4);
fun(&t); error:invalid conversion from 'const Time*' to 'Time*'
...
|
class Time{
public:
Time(int,int,int);
int hour;
…
};
void fun(Time &t){
t.hour=11;
}
int main(){
void fun(Time &);
Time t(2,3,4);
fun(t);
…
|
void fun(const Time &t){
t.hour=11; error:assignment of data-member 'Time::hour' in read-only structure
}
int main(){
void fun(const Time &);
Time t(2,3,4);
fun(t);
…
|
const int ci=1;//常變量
const int *const ci_p=&ci;//指向常量的指針常量
const int &ci_r=ci;//常引用
|