Created
June 26, 2017 16:32
-
-
Save bblanchon/4713e9724d034b592fab2bc59016c52d to your computer and use it in GitHub Desktop.
Pass by value or pass by reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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