views:

188

answers:

6

I want to implement a derived class that should also implement an interface, that have a function that the base class can call. The following gives a warning as it is not safe to pass a this pointer to the base class constructor:

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

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

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

    IInterface* m_inter;
};

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

    FuncToCall() {}
};

What is the best way around this? I need to supply the interface as an argument to the base constructor, as it is not always the dervied class that is the interface; sometimes it may be a totally different class.

I could add a function to the base class, SetInterface(IInterface* inter), but I would like to avoid that.

+1  A: 

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
{
    Derived() : Base() {}

public:
    static Derived createInstance() {
        Derived instance();
        instance.setInterface(instance);
        return instance;
    }

    FuncToCall() {}
};

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

Péter Török
+1  A: 

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.

David Rodríguez - dribeas
+3  A: 
Nikolai N Fetissov
This works only if the derived class is the interface implementor, which is not always true.
Péter Török
It also works if derived class either contains the interface implementation or holds a pointer to it. Just change the `GetInterface()` implementation.
Nikolai N Fetissov
A: 

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() {}
};
afriza
A: 

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).

Matthieu M.
A: 

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

Nissim