Created
April 7, 2015 04:20
-
-
Save jesboat/39c19ce74ecea7606cd0 to your computer and use it in GitHub Desktop.
demo of pass-by-value vs pass-by-reference in C++
This file contains 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> | |
#include <string> | |
using std::string; | |
void passByVal(string passBy) { | |
passBy = "reference"; | |
} | |
void passByRef(string &passBy) { | |
passBy = "reference"; | |
} | |
int main(int argc, char **argv) { | |
{ | |
string passBy = "value"; | |
passByVal(passBy); | |
std::cout << "passByVal is pass-by-" << passBy << std::endl; | |
} | |
{ | |
string passBy = "value"; | |
passByRef(passBy); | |
std::cout << "passByRef is pass-by-" << passBy << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment