Created
June 20, 2017 18:15
-
-
Save gerco/b44fb11da3c03e441b6a291ae31a5ffd to your computer and use it in GitHub Desktop.
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 <memory> | |
using namespace std; | |
static int mycount = 0; | |
class SomeObject { | |
private: | |
public: | |
int value; | |
int ncpy; | |
SomeObject() { | |
value = mycount++; | |
ncpy = 0; | |
cout << " Created " << *this << endl; | |
} | |
SomeObject(const SomeObject& that) { | |
this->value = that.value; | |
this->ncpy = that.ncpy+1; | |
cout << " Making " << *this << endl; | |
} | |
~SomeObject() { | |
cout << " Destroying " << *this << endl; | |
} | |
friend ostream& operator<<(ostream& str, const SomeObject& obj); | |
}; | |
ostream& operator<<(ostream& str, const SomeObject& obj) { | |
if(obj.ncpy > 0) { | |
str << "copy " << obj.ncpy << " of "; | |
} | |
str << "SomeObject(" << obj.value << ")"; | |
return str; | |
} | |
void byref(SomeObject& obj) { cout << " Received: " << obj << endl; } | |
void byptr(SomeObject* obj) { cout << " Received: " << *obj << endl; } | |
void byval(SomeObject obj) { cout << " Received: " << obj << endl; } | |
unique_ptr<SomeObject> createuqptr() { return unique_ptr<SomeObject>(new SomeObject()); } | |
SomeObject* createbyptr() { return new SomeObject(); } | |
SomeObject createbyval() { return SomeObject(); } | |
void testcreates() { | |
cout << "Returning a reference" << endl; | |
auto obj1 = createuqptr(); | |
cout << "Returning a pointer" << endl; | |
SomeObject* obj3 = createbyptr(); | |
delete obj3; | |
cout << "Returning a value" << endl; | |
SomeObject obj4 = createbyval(); | |
cout << "End of create tests" << endl; | |
} | |
void testcalls() { | |
SomeObject obj; | |
cout << "Calling with reference, this should NOT copy" << endl; | |
byref(obj); | |
cout << "Calling with pointer, this should NOT copy" << endl; | |
byptr(&obj); | |
cout << "Calling by value, this should copy" << endl; | |
byval(obj); | |
cout << "Doing some & assignments" << endl; | |
SomeObject& a = obj; | |
SomeObject& b = a; | |
cout << "Doing some value assignments" << endl; | |
SomeObject c = obj; | |
SomeObject d = c; | |
cout << "auto from reference" << endl; { | |
SomeObject& refToObj = obj; | |
auto cpy = refToObj; | |
} | |
cout << "auto& from reference" << endl; { | |
SomeObject& refToObj = obj; | |
auto& cpy = refToObj; | |
} | |
/* | |
cout << "NotCopyable auto from reference" << endl; { | |
NotCopyable obj2; | |
obj2.value = 43; | |
NotCopyable& refToObj2 = obj2; | |
auto cpy = refToObj2; | |
} | |
*/ | |
cout << "End of call tests" << endl; | |
} | |
int main(int argc, char * argv[]) { | |
cout << "========================= TESTING CALLS =========================" << endl; | |
testcalls(); | |
cout << "======================== TESTING CREATES ========================" << endl; | |
testcreates(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment