mardi 22 mars 2022

Regarding R values

#include <iostream>
#include <ctime>
#include <chrono>
#include "Person.h"
#include "SmartPerson.h"
using namespace std;

void print_name(const Person& test);
void print_name_2(const Person&& test);

int main()
{
    print_name(Person{ 21, "Juan", "Hispanic" });

    return 0;
}

void print_name(const Person& test)
{
    cout << "L value!" << endl;
    cout << test.get_name() << endl;
}
void print_name_2(const Person&& test)
{
    cout << "R value!" << endl;
    cout << test.get_name() << endl;
} 

Why is the function print_name called instead of print_name_2 in the above case? Even though it was an R-value being passed? I also want to know, what the purpose of a reference to a constant R-value is.

Aucun commentaire:

Enregistrer un commentaire