How to set base class members before derived class

2019-04-17 09:09发布

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?

3条回答
\"骚年 ilove
2楼-- · 2019-04-17 10:10

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.

查看更多
爷的心禁止访问
3楼-- · 2019-04-17 10:13

The base-class constructor is called before the derived class constructor, so set the values in your base-class constructor.

查看更多
祖国的老花朵
4楼-- · 2019-04-17 10:13

I'm not quite sure what you're tryign to accomplish here but I can make a few observations. It looks like you may be trying to implement two-phase construction.

First, if you want to use the base class constructor you will have to pass through the values from the derived constructor. There's no other mechanism in the language to do it, so if that's not an option (or at least not one you want to entertain) then you'll have to use an additional external interface.

To answer your final question, the members of the base are always initialized before the derived constructor is called. I'll assume you want to make sure they're set before using Update for example.

What about making Init protected, and renaming Set to something like SetAndInit and forcing it to always do the set before it calls the Init function. That prevents you from calling Init before the base members are set and still gives you a relatively similar interface.

查看更多
登录 后发表回答