Home > Net >  how to create factory function (returning rvalue)
how to create factory function (returning rvalue)

Time:09-17

I wrote this piece of code:

class widget_12 {
public:
    //reference qualifiers example
    void dowork()&
    {
        std::cout << "*this is lvalue\n";
    }; // this version of dowork applies when *this is lvalue
    void dowork()&&
    {
        std::cout << "*this is rvlaue\n";
    }; // -||- *this is rvalue
    widget_12&& make_widget()
    {
        //TODO
    }
};

I want to test reference qualifiers, therefore I need to create a function that will return the rvalue of widget_12, could you show me how to do it?

I am basically trying to make this call:

make_widget().dowork();

CodePudding user response:

Were you trying something like this?

https://godbolt.org/z/TPEWe3eKv

#include <iostream>

class widget_12 {
public:
    //reference qualifiers example
    void dowork() &
    {
        std::cout << "*this is lvalue\n";
    }; // this version of dowork applies when *this is lvalue

    void doword() &&
    {
        std::cout << "*this is rvlaue\n";
    }; // -||- *this is rvalue

    static widget_12 make_widget()
    {
        return widget_12();
    }
};

int main()
{
    widget_12 w;
    w.dowork();
    //w.doword(); // error
    widget_12::make_widget().doword();
    //widget_12::make_widget().dowork(); // error
    return 0;
}
  • Related