c++iosxcodeobjective-c++irrlicht

Returning reference to local temporary object


This code

virtual const core::matrix4& getViewMatrixAffector() const {return core::matrix4();};

results with a warning telling me "Returning reference to local temporary object"...

How to solve this warning?

As mentioned below i tried to remove the '&'... Error


Solution

  • Since you're not in control of the return type, you must make sure you return a valid object and not just a temporary. One solution would be a function-local static variable:

    virtual const core::matrix4& getViewMatrixAffector() const
    {
      static const core::matrix4 val;
      return val;
    };
    

    If you find yourself doing this in many functions (with the same type of the variable), make val a (suitably renamed) static member of the class.