- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我必须序列化基类和派生类的 CList
。我怎样才能做到这一点。
typedef CList<Student*, Student*> StVec;
class Student : public CObject
{
public:
DECLARE_SERIAL(Student);
Student();
Student(Student& s);
Student(CString _name, CString _last, int _age);
virtual ~Student(void);
virtual void cin();
virtual void cout();
virtual void Serialize(CArchive& ar);
static Student* FromInput();
inline const CString GetName(){return name;}
inline const CString GetLastName(){return last_name;}
inline const int GetAge(){return age;}
virtual inline Student& operator=( const Student &s )
{
name = s.name;
last_name = s.last_name;
age = s.age;
return *this;
}
protected:
CString name;
CString last_name;
int age;
};
class Warden : public Student
{
protected:
virtual void Serialize(CArchive& ar);
UINT salary;
};
class Group : public CObject
{
public:
DECLARE_SERIAL(Group);
enum FindType { First = 0, Last, All};
typedef bool (*StudentsFindCallback) (Student* student, void* condition);
Group(){ name = _T("Default Group"); }
Group(CString _name);
Group(Group& s);
~Group();
void CreateUser();
void ShowUsers();
//StVecOfIt FindUser(StudentsFindCallback f, void* condition, FindType ft);
inline Student* getStudent(POSITION pos) {return students->GetNext(pos); }
inline void DeleteUser(POSITION it){ students->RemoveAt(it); }
virtual void Serialize(CArchive& ar);
void Clean();
/*static bool FindbyName(Student* student, void* condition);
static bool FindbyLastName(Student* student, void* condition);
static bool FindbyAge(Student* student, void* condition);*/
virtual inline Group& operator=( const Group &s )
{
name = s.name;
students = s.students;
return *this;
}
protected:
CString name;
StVec* students;
};
void Student::Serialize(CArchive& ar)
{
CObject::Serialize( ar );
if (ar.IsStoring())
{
ar.SerializeClass(Student::GetRuntimeClass());
ar << name << last_name << age;
}
else
{
ar.SerializeClass(Student::GetRuntimeClass());
ar >> name >> last_name >> age;
}
}
void Group::Serialize(CArchive& ar)
{
ar.SerializeClass(RUNTIME_CLASS(Group));
if (ar.IsStoring())
{
ar << (int)students->GetCount();
POSITION pos = students->GetHeadPosition();
while(pos != NULL)
{
Student* student = students->GetNext(pos);
student->Serialize(ar);
}
}
else
{
Clean();
int count = 0;
ar >> count;
for(INT_PTR i = 0; i < count; ++i)
{
Student* st = new Student();
st->Serialize(ar);
students->AddTail(st);
}
}
}
我在 codeproject 找到了一篇文章但我根本无法理解。
当然我可以添加一个标志来指示类的类型。我也可以序列化到派生类,如果派生属性为空,则使用 static_cast
将其派生到基类。或者我正在考虑使用 CArchive::ReadObject
或 CArchive::ReadClass
来加载类,但是还有其他方法吗?
最佳答案
我的 MFC 生锈了,但我相信这应该可以工作(未经测试!)。
此外,您的 Serialize() 方法不应该为它自己的类调用 SerializeClass。由调用者决定调用它。例如,<< 和 >> 运算符用于识别存档中下一个对象的类。
void Student::Serialize( CArchive& ar )
{
CObject::Serialize( ar );
if (ar.IsStoring())
{ // No need for SerializeClass here.
ar << name << last_name << age;
}
else
{ // No need for SerializeClass here.
ar >> name >> last_name >> age;
}
}
void Group::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << (int)students->GetCount();
POSITION pos = students->GetHeadPosition();
while(pos != NULL)
{
Student* student = students->GetNext(pos);
ar << student; // << includes class info about the serialized object
}
}
else
{
Clean();
int count = 0;
ar >> count;
for(INT_PTR i = 0; i < count; ++i)
{
CObject *p;
ar >> p; // Deserialize whatever kind of object comes next
students->AddTail(p);
}
}
}
关于c++ - MFC序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12629476/
我有一个 MFC 项目,它链接到第三方 dll。在程序退出时,IDE 会报告“检测到内存泄漏!”并倾倒泄漏物。 这些泄漏来自第三方 dll。我很确定这些是错误的报道。 (Google 的快速检查表明
首先我想要的是:能够显示具有多列的网格,每个单元格都有一个自定义渲染回调。因此,您可以使用这样的控件在游戏中显示您的库存,或者类似于 Google Chrome 中的行为,它会显示您访问的热门页面的网
在新的 MFC 功能(功能包)中,菜单出现在三个地方: 在菜单栏中 (CMFCMenuBar) 在弹出菜单中 (CMFCPopupMenu) 在 CMFCButton 的“下拉菜单”版本中 我想在所有
在我的 MFC 项目中,我想动态生成标签。例如:我必须为它们生成 4 个编辑控件和相应的标签——比如“Label1”“Label2”...... CStatic *label[MAX_THREAD];
我有第一个对话框,上面有一个简单的按钮,单击该按钮时,使用 CDialog::Create(IDD,this) 创建了第二个对话框。我希望在销毁第二个对话框但不向第二个对话框添加任何代码时通知父级,即
我们的代码库中的添加到剪贴板代码非常低级 - 分配全局内存等等。对于简单的情况,我只想将一些纯文本放在剪贴板上,是否有任何例程可以包装所有这些内容? 一个例子是 CRichEditCtrl 具有 Co
我对 mfc 中事件和消息之间的区别有点困惑。 他们是一样的吗? 最佳答案 由于您专门询问 MFC,我假设您指的是可以在 MFC 类的属性窗口中定义的事件处理程序和消息处理程序。 在“事件”下,您可以
如何以编程方式检测我的 MFC 应用程序当前是否正在显示模式对话框或属性表?目前我正在使用以下内容,但我觉得该代码也会触发无模式对话框。 bool HasModalDialog(const CWnd*
我有两个按钮: 单选按钮:“十六进制” 按钮:“A” 我想在用户选中“十六进制”按钮时启用“A”(“A”在创建时的状态为“禁用”),我该怎么做?谢谢大家。 最佳答案 您需要使用 CButton 's
通常情况下,窗口的厚度为 4 个像素,可以通过 GetSystemMetrics 方法获取。我可以更改它的值,例如 2 个像素吗? 非常感谢! 最佳答案 简单的回答:不。不适用于特定窗口。 复杂的答案
我需要打开一个从同一个类实例化的对话框两次。当我尝试这个时 CdelmeDlg dlg; dlg.DoModal(); dlg.DoModal(); 第二个调用只打开对话框一瞬间,然后关闭。我敢打赌消
如何通过代码在 mfc 中减少标题栏的窗口? 最佳答案 用: ModifyStyle (WS_CAPTION, 0); // to hide ModifyStyle (0, WS_CAPTION);
我使用 AfxMessageBox 关键字显示消息框。基本的东西。 但是,由于路径的长度,它显示如下: 有什么方法可以阻止它以这种方式自动换行吗? 抱歉,如果这是一个愚蠢的问题。但如果我只有一个句子,
所以,我有一个 MFC 程序的插件。我正在使用鼠标事件 Hook (来自 SetWindowsHookEx)来捕获点击。主机应用程序可以打开任意数量的(可能重叠的)子窗口,但我只想拦截特定子窗口中的点
我正在尝试将图像添加到现有按钮..我在一定程度上做到了这一点,问题是我可以添加一个所有者绘制的图像,但无法添加我想要的确切图像..示例见下文代码 CButton* pBtn= (CButton*)Ge
我想在对话框的标题栏上放置一些控件。一种可能的解决方案是,我只是从对话框中删除标题栏并绘制自己的标题栏。 但是如果我自己画标题栏,标题栏的外观不会根据窗口的主题而改变。而且我也不得不忍受接近、最小化和
我已经使用 MinGW 开发了 WinAPI 应用程序,没有问题。现在,我可以用 MFC 做同样的事情吗? 最佳答案 你不能。 MFC 代表 Microsoft Foundation Classes,
(我正在使用VS ++ 2005) 我在对话框上放置了编辑框控件(ID为ID_edit_box),并为其关联了两个变量(使用处理程序向导):控件(c_editbox)和值(v_editbox)变量。我
我有一个自定义 MFC 对话框 CMyDialog,使用资源编辑器添加了一个类型为 CMyControl 的自定义控件 - 该对话框有一个控件的成员变量并且有 DDX设置。 该控件正在接收绘制消息,并
直到现在,我从未真正需要我正在开发的大型 MFC(如果重要,则为单文档界面)应用程序的 Winapp ExitInstance()。但现在我这样做了,主要是为了清理内存分配、卸载一些 DLL 等。好吧
我是一名优秀的程序员,十分优秀!