我知道有些人会说这是对象切片问题,但我不这么认为。我在这个网站上看到了很多相关的帖子,但并不完全相同。让我们从代码开始:
#include "stdafx.h"
#include <list>
struct MY_STRUCT
{
int a;
int b;
};
class File
{
public:
virtual void Load(char * fileName) = 0;
};
class ExcelFile : public File
{
protected:
std::list<MY_STRUCT> my_list;
public:
ExcelFile(){};
~ExcelFile(){};
virtual void Load(char * fileName)
{
// load the file into my_list
}
};
int _tmain(int argc, _TCHAR* argv[])
{
char fileName[] = "test.txt";
File * file = new ExcelFile;
file->Load( fileName );
// Now I need to fill a Windows List or CListView from my_list data but how?
// I can't access or iterate my_list here and I am not too sure if
// should pass a windows object to the class to fill it up?
// Even if I iterate by adding a member function to return the list object, wouldn't not
// it violate the data encapsulation rule thereby defeating the purpose of having
// interface class?
return 0;
}
所以基本上我有一个接口类,其派生类具有聚合(集合)中的数据。现在我想显示数据。这样做的正确方法是什么?我在代码的注释中提到了这个问题......我想我在写这篇文章时已经找到了答案,我应该让类添加填充列表的函数。而且我想如果我必须填写一个 ListBox 或 ListView 比我需要两个函数一个列表。我想知道我是否可以用访客模式做得更好!?