Skip to content

Instantly share code, notes, and snippets.

@bblanchon
Created June 26, 2017 16:32
Show Gist options
  • Save bblanchon/4713e9724d034b592fab2bc59016c52d to your computer and use it in GitHub Desktop.
Save bblanchon/4713e9724d034b592fab2bc59016c52d to your computer and use it in GitHub Desktop.
Pass by value or pass by reference
#include <iostream>
struct String
{
String()
{
}
String(const String&)
{
std::cout << " copy" << std::endl;
}
String(const String&&)
{
std::cout << " move" << std::endl;
}
};
struct ByReference
{
ByReference(const String& s) : _str(s) {}
String _str;
};
struct ByValue
{
ByValue(String s) : _str(std::move(s)) {}
String _str;
};
int main()
{
String s;
std::cout << "Pass by reference:" << std::endl;
std::cout << " Contruct form lvalue" << std::endl;
ByReference c(s);
std::cout << " Contruct form rvalue" << std::endl;
ByReference d(String{});
std::cout << "Pass by value:" << std::endl;
std::cout << " Contruct form lvalue" << std::endl;
ByValue a(s);
std::cout << " Contruct form rvalue" << std::endl;
ByValue b(String{});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment