How to set base class members before derived class constructor is called?(如何在调用派生类构造函数之前设置基类成员?)
问题描述
There is a base class for components of my application which provides some members and the functions Init()
and Update()
which must be overwritten.
class Component
{
public:
void Set(type* Member1, type* Member2, type* Member3)
{
this->Member1 = Member1;
this->Member2 = Member2;
this->Member3 = Member3;
}
virtual void Init() = 0;
virtual void Update() = 0;
virtual ~Component() {}
protected:
type* Member1;
type* Member2;
type* Member3;
};
For handeling the components, there is a manager. It first sets the members of a component and then calls Init()
on it. Later on it can be used to update all assigned components but that isn't related to this question.
class Manager
{
public:
void Add(string Name, Component* Component)
{
list[Name] = Component;
}
void Init(type* Member1, type* Member2, type* Member3)
{
for (auto i = list.begin(); i != list.end(); i++)
{
i->second->Set(Member1, Member2, Member3);
i->second->Init();
}
}
void Update()
{
for (auto i = list.begin(); i != list.end(); i++)
i->second->Update();
}
private:
unordered_map<string, Component*> list;
};
I am not really happy with my implementation since I would like components to use their constructor for initialization instead overwriting the Init()
function. But at construction time the base class member aren't available yet.
I know that I could pass the members through the derived class constructor, but I do not want specific components to care about their base class' members. That would look like the following but anyway I do not want that.
class Component
{
public:
Component(type* Member1, type* Member2, type* Member3)
{
this->Member1 = Member1;
this->Member2 = Member2;
this->Member3 = Member3;
}
virtual void Update() = 0;
virtual ~Component();
protected:
type* Member1;
type* Member2;
type* Member3;
};
class Specific : public Component
{
public:
Specific(type* Member1, type* Member2, type* Member3) : Component(Member1, Member2, Member3)
{
}
void Update()
{
}
};
So how can I insure that the base class members are initialized before calling the constructor of the derived class?
You don't really have a choice. Either the derived class knows about the initialisation needs of the base class (because the base class needs that information in its constructor/initialisation function), or you have to move the derive-class' initialisation out of its constructor (to be called by the client after it initialised the base class).
If the list of members that need to be set on the base class is long, you could package them all in a structure and pass that, via the derived class, to the base class.
这篇关于如何在调用派生类构造函数之前设置基类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在调用派生类构造函数之前设置基类成员?


基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01