Answers for "c++ overloading by ref-qualifiers"

C++
0

c++ overloading by ref-qualifiers

#include <stdio.h>

class My {
public:
    int get(int) & { // notice &
        printf("returning int..\n");
        return 42;
    }
    char get(int) && { // notice &&
        printf("returning char..\n");
        return 'x';
    };
};

int main() {
    My oh_my;
    oh_my.get(13); // 'oh_my' is an lvalue
    My().get(13); // 'My()' is a temporary, i.e. an rvalue
}
Posted by: Guest on October-09-2021

Browse Popular Code Answers by Language