Skip to content

Instantly share code, notes, and snippets.

@jesboat
Created April 7, 2015 04:20
Show Gist options
  • Save jesboat/39c19ce74ecea7606cd0 to your computer and use it in GitHub Desktop.
Save jesboat/39c19ce74ecea7606cd0 to your computer and use it in GitHub Desktop.
demo of pass-by-value vs pass-by-reference in C++
#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