this pointer to base class constructor?

懵懂的女人 提交于 2019-11-29 11:12:22

You shold not publish this from the constructor, as your object is not yet initialized properly at that point. In this actual situation, though, it seems to be safe, since you are publishing it only to the base class, which only stores it and does not invoke it until some point later, by which time the construction will have been finished.

However, if you want to get rid of the warning, you could use a static factory method:

struct Base
{
public:
    Base() { }

    void setInterface(IInterface* inter) { m_inter = inter; }

    void SomeFunc() { inter->FuncToCall(); }

    IInterface* m_inter;
};

struct Derived : Base, IInterface
{
private:
    Derived() : Base() {}

public:
    static Derived* createInstance() {
        Derived instance = new Derived();
        instance->setInterface(instance);
        return instance;
    }

    FuncToCall() {}
};

Note that the constructor of Derived is private to ensure that instantiation is done only via createInstance.

You can always defer the interface dereference:

struct IInterface
{
    virtual ~IInterface();
    virtual void FuncToCall() =0;
};

class Base { public: virtual ~Base(); void SomeFunc() { GetInterface().FuncToCall(); } private: virtual IInterface& GetInterface() =0; };

class Derived: public Base, public IInterface { public: private: virtual IInterface& GetInterface() { return *this; } virtual void FuncToCall(); };

There is a limited set of operations that you can do (guaranteed by the standard) with a pointer to a yet uninitialized object, and storing it for further use is one of them. The compiler is probably warning as it is easy to misuse the received pointer in Base.

Beware that most uses of the pointer for other than storage will be undefined behavior, but the code above is correct.

What is quite funny is that you could get away with it by initializing it later on:

Derived::Derived(): Base()
{
  this->setInter(this);
}

is fine, because all attributes have been initialized.

This way you won't have to change your whole design just to get away with the warning.

However, unless the setInter does not do anything with this apart some storage, you might access an object you did not fully initialized (so storing a hash value could be awkward).

how about:

struct IInterface
{
    void FuncToCall() = 0;
    IInterface* me() { return this; }
};

...

struct Derived : IInterface, Base
{
    Derived() : IInterface(), Base(me()) {}

    FuncToCall() {}
};

at the point we get to Base constructor IInterface is already initialized, so the call to me() is safe, and warning free

what about using protected: for IInterface* m_inter;

struct IInterface
{
    virtual void FuncToCall() = 0;
};

struct Base
{
    Base(IInterface* inter) { m_inter = inter; }

    void SomeFunc() { m_inter->FuncToCall(); }

protected: // or `public:` since you are using `struct`
    IInterface* m_inter;
};

struct Derived : Base, IInterface
{
  //Derived() : Base(this) {} // not good to use `this` in the initialization list
    Derived() : Base() { m_inter = static_cast<IInterface*>(this); }

    FuncToCall() {}
};
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!