![主題發起人bjk註記此篇回應為最佳解答 主題發起人bjk註記此篇回應為最佳解答](https://imageproxy.pixnet.cc/imgproxy?url=https://www.programmer-club.com.tw/pc2020v5/images/Mark/I039.gif) |
2005/7/2 下午 01:32:15
>int fun(const int a) 表示不可以修改a
>int fun(int a) const 表示這個function可以讀取 const objet
>請問
>const int fun(int a)的const作用是什麼
>
>問二
>
>int fun(int a) const
>這種作法是不是只有在class才能用ㄚ
>class Time
>{
>int getHour() const;
>}
>
試考慮以下情況
class A {
. ....
void doTest() const
void doTest2();
};
class B {
.....
const A getClassA() {....}
};
int main()
{
B objB;
A objA = objB.getClassA(); // compile error,
const A objA2 = objB.getClassA(); // compile ok
objA2.doTest(); // compile ok
objA2.doTest2(); // compile error
};
以 int 變數接受 回傳 const int function 並不會有什麼問題
但若要接受回傳const的類別時, 則必需要以 const 物件來接受
該 const 物件只能使用其 const member function
|
|