vendredi 18 décembre 2020

The object adress of a return object with the type of boost::function &

class Ctx
{
public:
const boost::function<void((void*))>& Ctx::getBoostHookFun(void)
{
    std::cout << "inner hookFunc4Boost:" << (void*)(&hookFunc4Boost) << std::endl;
    return hookFunc4Boost;
}

private:
boost::function<void(void*)> hookFunc4Boost;

}

int main()
{
    const boost::function<void((lua_State*, lua_Debug*))> hookFunc = pCtx->getBoostHookFun();
    std::cout << "outer hookFunc:" << (void*)(&hookFunc) << std::endl;
}

Here are the outputs:

inner hookFunc4Boost:0x7fffffffdf28
outer hookFunc:0x7fffffffd540

Why the addresses of this two variable(i.e hoofunc and Ctx::hookFunc4Boost) are not same whereas the Ctx::getBoostHookFun() return a left reference to Ctx::hookFunc4Boost?

I intend to avoid the copy of a temporary object by returning a left reference to Ctx::hookFunc4Boost.

Aucun commentaire:

Enregistrer un commentaire