Last active
November 6, 2015 20:12
-
-
Save troyane/18142361fb15f56fae43 to your computer and use it in GitHub Desktop.
C++ example of passing arguments as references
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> | |
using namespace std; | |
struct Container { | |
int value; | |
explicit Container(const int i) : value(i) { | |
cout << "\nCreated Container per typical c-tor.\n"; | |
} | |
Container(const Container& another) : value(another.value) { | |
cout << "\nCreated Container per copy c-tor.\n"; | |
} | |
}; | |
void testObj(Container obj) { | |
cout << __FUNCTION__ << "\n\tpointer: " << &obj | |
<< "\n\tvalue: " << obj.value << endl; | |
} | |
void testPointer(const Container *p) { | |
cout << __FUNCTION__ << "\n\tpointer: " << p | |
<< "\n\tvalue: " << p->value << endl; | |
} | |
void testReference(const Container &r) { | |
cout << __FUNCTION__ << "\n\tpointer: " << &r | |
<< "\n\tvalue: " << r.value << endl; | |
} | |
int main() | |
{ | |
Container container(42); | |
cout << "- - -\n"; | |
cout << "Original\n\tpointer: " << &container | |
<< "\n\tvalue: " << container.value << endl; | |
cout << "- - -\n"; | |
testObj(container); | |
testPointer(&container); | |
testReference(container); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment