Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I have a problem using CList on mfc.
I'm trying to build a paint on mfc.

I have a Class for Shape, and derived class Rectangle and Circle.
on Shape i stored the color of the shape and on Rectangle I stored the 2 points to draw the rectangle.
I made a CList<cshape,cshape> list.
When I debug I see that only the color stored in the list(CShape) and not the points of the shape(CRectangle).
Why it happens?
Thanks!

C++
#pragma once
class CShape
{
public:
	CShape();
	CShape(int, int, int);
	virtual ~CShape();
	void setGreen(int);
	void setRed(int);
	void setBlue(int);
	int getRed();
	int getGreen();
	int getBlue();

protected:
	int red;
	int green;
	int blue;
};

#pragma once
#include "Shape.h"
class CRectangle :
	public CShape
{
public:
	CRectangle();
	CRectangle(CPoint , CPoint ,int , int, int);
	~CRectangle();
	CPoint getPoint1();
	CPoint getPoint2();
private:
	CPoint p1;
	CPoint p2;
};

//Some function...
.
.
.
  CRectangle r(CPoint(0, 0), CPoint(1, 1), 255, 255, 255);
  CList <CShape, CShape> l;
  l.AddHead(r);
.
.
.
Posted

You need to show the implementation of your constructors (and other functions). As it stands your classes do nothing.
 
Share this answer
 
Comments
w_ommer 10-Jan-15 23:47pm    
There is no implemntation in the constructors yes.
only this->x = x etc...
Richard MacCutchan 11-Jan-15 3:35am    
So where is the actual implementation that shows the creation of a CRectangle with the missing data members?
Shmuel Zang 11-Jan-15 3:48am    
It seems like the problem isn't in the classes' constructors but, in the list's declaration. (See my solution)
Richard MacCutchan 11-Jan-15 4:03am    
That's possible, but we still need to see the actual code to be sure.

That's because you store the items of your CList by value (your declaration is: CList <CShape, CShape> l;). In that way, when you add elements to your list the copy-constructor of CShape is called and copies only the data-members of CShape.


You may want to use pointers instead . Something like:


C++
CRectangle* r = new CRectangle(CPoint(0, 0), CPoint(1, 1), 255, 255, 255);
CList <CShape*> l;
l.AddHead(r);

// You should delete the CRectangle when you remove it from the list.

Or, a shared_ptr. Something like:


C++
std::shared_ptr<CRectangle> r(new CRectangle(CPoint(0, 0), CPoint(1, 1), 255, 255, 255));
CList <std::shared_ptr<CShape> > l;
l.AddHead(r);

// When you remove the CRectangle form the list, it is deleted by the shared_ptr destructor.
 
Share this answer
 
v3
Comments
w_ommer 17-Jan-15 4:36am    
hi,
Well it workes, but only when i do:
l.addHead(&object);

but when the object is deleted also the data in the list is gone...
what is the problem?
Shmuel Zang 17-Jan-15 13:54pm    
Do you wonder way the data of a deleted object is gone? Maybe because it is deleted...
You send &object as a parameter. - Does it a pointer to the stack (not a good idea...)?
If you want to store an object and delete it (and keep the data in the list), you should store a copy of the object (and, delete it when you remove it from the list).
w_ommer 17-Jan-15 14:35pm    
thaks...how do i do that?

just to remind, i didnt call any destructor or delete()...
Shmuel Zang 18-Jan-15 2:59am    
You should create the CRectangle on the heap (instead of on the stack). See the updated solution.
w_ommer 18-Jan-15 12:04pm    
GREAT it works!!
The first sugestion was good

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900