- Example

An example of rvalue and lvalue references in modern C++.

We'll cover the following...

Example

Press + to interact
// rvalueReference.cpp
#include <algorithm>
#include <iostream>
#include <string>
struct MyData{};
std::string function( const MyData & ) {
return "lvalue reference";
}
std::string function( MyData && ) {
return "rvalue reference";
}
int main(){
std::cout << std::endl;
MyData myData;
std::cout << "function(myData): " << function(myData) << std::endl;
std::cout << "function(MyData()): " << function(MyData()) << std::endl;
std::cout << "function(std::move(myData)): " << function(std::move(myData)) << std::endl;
std::cout << std::endl;
}
...
Access this course and 1400+ top-rated courses and projects.