?? 14-31.txt
字號:
/*范例14-31*/
#include <iostream.h>
class A
{
public:
int a;
A():a(5){cout<<a<<endl;}
void fun1() const // 常數(shù)成員函數(shù)
{
cout<<a<<endl;
}
void fun2() // 非常數(shù)成員函數(shù)
{
cout<<a<<endl;
}
};
void main()
{
const A obj1;//建立一常數(shù)對象
obj1.fun1();
obj1.fun2(); /* 常數(shù)對象使用非常數(shù)成員函數(shù),不同編譯器有不同的處理,以
下為Borland C++ Builder與Visual C++的處理方式 */
/*
1. Borland C++ Builder 編譯器會產(chǎn)生一警告
Non-const function A::fun2() called for const object.
2. Visual C++ 編譯器直接就產(chǎn)生錯誤
error C2662: 'fun2' : cannot convert 'this' pointer
from 'const class A' to 'class A &'
*/
getchar();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -