我有一个CPolygon 派生自班级的班级CElement.[我在这里利用多态].
class CElement : public CObject
{
public:
virtual ~CElement();
virtual void Draw(CDC* pDC){};
CPoint vertices[11];
protected:
CElement();
};
Run Code Online (Sandbox Code Playgroud)
class CPolygon : public CElement
{
public:
CPolygon(CPoint mFirstPoint,CPoint mSecondPoint);
~CPolygon(void);
virtual void Draw(CDC* pDC);
protected:
CPoint mStartPoint;
CPoint mEndPoint;
CPolygon(void);
};
Run Code Online (Sandbox Code Playgroud)
当我尝试将数组分配给verticesCElement对象的成员时,我收到错误:expression must be a modifiable Lvalue
CElement* a = new CPolygon(mFirstPoint,mSecondPoint);
a->vertices=vertices; //here!!
Run Code Online (Sandbox Code Playgroud)
为什么不工作?
因为a->vertices不是可修改的左值...你不能在C++中分配数组,你只能分配特定的元素或进行复制.
如果您知道大小11,我会使用std::array(或者std::vector,为了灵活性)而不是C风格的数组.