如何在派生类构造函数被调用之前设置基类成员? [英] How to set base class members before derived class constructor is called?

查看:135
本文介绍了如何在派生类构造函数被调用之前设置基类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的组件有一个基类,它提供了一些成员和函数 Init() Update()必须被覆盖。

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;
};

为了处理组件,有一个经理。它首先设置一个组件的成员,然后调用 Init()

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;
};

我不太满意我的实现,因为我想要组件使用它们的构造函数进行初始化, Init()函数。但是在构建时,基类成员还不可用。

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.

这篇关于如何在派生类构造函数被调用之前设置基类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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