标签:
首先看看下面这个例子:
1 class Point{ 2 public: 3 point(int x, int y); 4 ... 5 void setX(int newVal); 6 void setY(int newVal); 7 ... 8 }; 9 struct RectData{ 10 Point ulhc; //左上角 11 Point lrhc; //右下角 12 }; 13 class Rectangle{ 14 ... 15 private: 16 shared_ptr<RectData> pData; 17 };
那么如果出现下面这种成员函数的话:
1 class Rectangle{ 2 public: 3 ... 4 Point & upperLeft() const {return pData->ulhc; } 5 Point & lowerRight() const{return pData->lrhc; } 6 ... 7 }
1 class Rectangle{ 2 public: 3 ... 4 const Point & upperLeft() const {return pData->ulhc; } 5 const Point & lowerRight() const{return pData->lrhc; } 6 ... 7 }
但是几十这样做仍然可能会导致造成虚调handle的现象发生:
class GUIobject{...}; const Rectangle boundingBox(const GUIobject & obj); //那么,当用户这样去使用他的时候 GUIobject * pgo; ... const Point * pUpperLeft = &(boundingBox(*pgo).pUpperLeft());
标签:
原文地址:http://www.cnblogs.com/-wang-cheng/p/4872324.html