2013-02-07 22 views
0
class Point { 
public: 
    Point(int x, int y) : { x = new int(x); y = new int(y) } 
    ... 
    ... 
    Point& operator=(const Point& other) { 
     if(this!=&other){ 
      delete x; 
      delete y; 
      x = new int(*other.x); 
      y = new int(*other.y); 
     } 
     return *this; 
    } 
private: 
    const int* x; 
    const int* y; 
} 

即使x和y已經被初始化,operator =這個實現是否仍然工作?刪除const指針是否允許我們重新分配它?常量指針的成員和運算符=

+2

爲什麼你首先要拿指針? – juanchopanza

+0

來自我的課程 – TheNotMe

+5

以前的考試之一的問題這不是一個常量指針,而是一個指向常量的指針。 –

回答

5

這不是const指針,而是指向const的指針。所以你可以修改指針,你不能指向它。

一個const指針

int* const x; 

和你的代碼將無法再編譯。

+0

Ohhhh非常感謝!抱歉我的困惑! – TheNotMe