简体   繁体   中英

Why can't I assign a value to a member of the base class?

I have a class CPolygon which is derived from the class CElement . [ I'm making use of polymorphism here].

 class CElement : public CObject
 {
 public:
virtual ~CElement();
virtual void Draw(CDC* pDC){};
CPoint vertices[11];

 protected:

CElement();

 };

 class CPolygon : public CElement
 {
 public:
CPolygon(CPoint mFirstPoint,CPoint mSecondPoint);
~CPolygon(void);
 virtual void Draw(CDC* pDC);                 


 protected:
CPoint mStartPoint;
CPoint mEndPoint;
CPolygon(void);


 };

When I try to assign an array to the member vertices of a CElement object, I get the error: expression must be a modifiable Lvalue

 CElement* a = new CPolygon(mFirstPoint,mSecondPoint);
  a->vertices=vertices;        //here!!

Why doesn't this work??

Because a->vertices is not a modifiable Lvalue... You can't assign arrays in C++, you can only assign specific elements or do a copy.

If you know the size to be 11 , I'd use a std::array (or std::vector , for flexibility) instead of a C-style array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM