关于mfc问题的CList [英] CList on mfc problems

查看:77
本文介绍了关于mfc问题的CList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我在mfc上使用CList时遇到问题。

我正在尝试在mfc上建立一个油漆。



我有一个Shape类,派生类Rectangle和Circle。

在Shape i上存储了形状的颜色,在Rectangle上我存储了2个点来绘制矩形。

我做了一个CList< cshape,cshape>列表。

当我调试时,我只看到列表中存储的颜色(CShape)而不是形状点(CRectangle)。

为什么会发生?

谢谢!



  #pragma once 
CShape
{
public
CShape ();
CShape( int int int < /跨度>);
虚拟 ~CShape();
void setGreen( int );
void setRed( int );
void setBlue( int );
int getRed();
int getGreen();
int getBlue();

受保护
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;
};

// 某些功能......



CRectangle r(CPoint( 0 0 ),CPoint( 1 1 ), 255 255 255 );
CList< CShape,CShape>升;
l.AddHead(r);


解决方案

您需要显示构造函数(和其他函数)的实现。你的课程什么都不做。


那是因为你按价值存储你的 CList 的项目(你的声明是: CList< CShape,CShape> l; )。这样,当您向列表中添加元素时,将调用 CShape 的复制构造函数,并仅复制 CShape的数据成员



您可能想要使用指针。类似于:

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

// 从列表中删除CRectangle时应该删除它。

或者, shared_ptr 。类似于:

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

// 当您从列表中删除CRectangle时,它将被shared_ptr析构函数删除


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!

#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);
.
.
.

解决方案

You need to show the implementation of your constructors (and other functions). As it stands your classes do nothing.


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:

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:

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.


这篇关于关于mfc问题的CList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆