Skip to content

Instantly share code, notes, and snippets.

@troyane
Last active November 6, 2015 20:12
Show Gist options
  • Save troyane/18142361fb15f56fae43 to your computer and use it in GitHub Desktop.
Save troyane/18142361fb15f56fae43 to your computer and use it in GitHub Desktop.
C++ example of passing arguments as references
#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