1
下面是以下程序。這只是一個基於2分的矩形。我的問題是矩形構造函數。智能指針到用戶定義結構的成員初始化列表
#include <iostream>
#include <memory>
class Point { // class for representing points
public:
Point(int x, int y);
void setX(int newVal);
void setY(int newVal);
};
struct RectData { // Point data for a Rectangle
Point _ulhc; // ulhc = 「 upper left-hand corner」
Point _lrhc; // lrhc = 「 lower right-hand corner」
};
class Rectangle {
public:
Rectangle(Point ulhc, Point lrhc) :
_pData->_ulhc(ulhc), _pData->_lrhc(lrhc)
{}
Point & upperLeft() const { return _pData->_ulhc; }
Point & lowerRight() const { return _pData->_lrhc; }
private:
std::tr1::shared_ptr<RectData> _pData;
};
int main()
{
Point coord1(0, 0);
Point coord2(100, 100);
const Rectangle rec(coord1, coord2); // rec is a const rectangle from
// (0, 0) to (100, 100)
rec.upperLeft().setX(50); // now rec goes from
// (50, 0) to (100, 100)!
return 0;
}
因爲看起來我沒有正確地進行初始化。 MSVC給我錯誤expected a '(' or a '{'
。我很困惑。如何通過此構造函數正確初始化_pData
結構?