我想创建一个类,将成员对象传递给它的父对象进行初始化。下面的代码显示了我想要做的事情。如何将类的成员对象传递给其基类的构造函数?
class TQueueViewerForm1 : public TQueueViewerForm
{
private: // User declarations
DOMMsgCollectionEditorImpl m_collection;
public: // User declarations
__fastcall TQueueViewerForm1(TComponent* Owner);
};
__fastcall TQueueViewerForm1::TQueueViewerForm1(TComponent* Owner)
: TQueueViewerForm(Owner, m_collection)
{
}
但是,这似乎并不奏效。它看起来像构造函数TQueueViewerForm()在m_collection被初始化之前被调用。由于TQueueViewerForm()尝试使用未初始化的对象,因此这会使程序崩溃。
那么......我在这里做了什么选择?理想情况下,我想在父类初始化之前初始化m_collection。
哦,哇。这将解决这个问题,但我不认为我可以原谅自己写这种代码... – QuestionC
同意。这看起来过于复杂,并且可能不值得为不熟悉代码库的新维护人员进行维护。最好重新考虑问题并尝试不同的方法。 – jia103