Created
July 14, 2016 00:02
-
-
Save rgoulter/b33b54ecc6f17e454387234d941e229b 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 <vector> | |
using namespace std; | |
class MyObj { | |
public: | |
MyObj(int x) { | |
cout << " CONS Constructing MyObj(" << x << ")" << endl; | |
x_ = x; | |
} | |
~MyObj() { | |
cout << " DEST Destructing MyObj(" << x_ << ")" << endl; | |
} | |
MyObj(const MyObj& other) { | |
x_ = other.x(); | |
cout << " CPY Copying from " << x_<< endl; | |
} | |
void setX(int x) { x_ = x; } | |
int x() const { return x_; } | |
MyObj& operator=(const MyObj& rhs) { | |
cout << " ASSG Assigning from " << rhs.x() << " to " << x_ << endl; | |
if (this == &rhs) return *this; | |
x_ = rhs.x(); | |
return *this; | |
} | |
private: | |
int x_; | |
}; | |
MyObj f1() { | |
cout << " f1():" << endl; | |
return MyObj(7); | |
} | |
void outp(MyObj o) { | |
cout << " Output MyObj(" << o.x() << ")" << endl; | |
} | |
void outpC(const MyObj& o) { | |
cout << " Output MyObj(" << o.x() << ")" << endl; | |
} | |
void vectorStuff() { | |
cout << "vectorStuff: Begin" << endl; | |
vector<MyObj> vec; | |
MyObj o1(100); | |
cout << "vectorStuff: insert 1" << endl; | |
vec.push_back(o1); // CPY | |
cout << "vectorStuff: insert 2" << endl; | |
vec.push_back(MyObj(200)); // CONS200 + CPY200, + CPY100 + DEST100,200 | |
cout << "vectorStuff: insert 3" << endl; | |
vec.push_back(MyObj(300)); // CONS300 + CPY300 + CPY100,200 + DEST1,2,3 | |
cout << "vectorStuff: insert 4" << endl; | |
vec.push_back(MyObj(400)); // CONS400 + CPY400 + DEST400 | |
cout << "vectorStuff: Done adding" << endl; | |
cout << "vectorStuff: o1:" << endl; | |
// Does nothing (to result below), | |
// since o1 doesn't refer to the copy the vector has. | |
o1.setX(150); | |
cout << "vectorStuff: o2:" << endl; | |
MyObj o2 = vec[1]; // CPY | |
// Does nothing (to the result below), | |
// since o2 is a copy from the vector | |
o2.setX(250); | |
cout << "vectorStuff: o3:" << endl; | |
MyObj& o3 = vec[2]; // no cpy | |
// modifies the current copy in vector | |
o3.setX(350); | |
cout << "vectorStuff: o4:" << endl; | |
// modifies the current copy in vector | |
vec[3].setX(450); // no cpy | |
cout << "vectorStuff: Done mutating" << endl; | |
outpC(vec[0]); // 100 | |
outpC(vec[1]); // 200 | |
outpC(vec[2]); // 350 | |
outpC(vec[3]); // 450 | |
cout << "vectorStuff: End" << endl; | |
} | |
int main(int argc, char *argv[]) { | |
cout << "Beginning of main()" << endl; | |
/// Copying in initialisation? | |
cout << " x0 = MyObj(10):" << endl; | |
MyObj x0 = MyObj(10); // CONS, no cpy | |
cout << " x1(5):" << endl; | |
MyObj x1(5); // CONS | |
cout << " x2(x1):" << endl; | |
MyObj x2(x1); // CPY (duh) | |
cout << " x3 = x1:" << endl; | |
MyObj x3 = x1; // CPY | |
cout << endl; | |
/// Copying in return from functions? | |
cout << " x4 = f1():" << endl; | |
MyObj x4 = f1(); // CONS, no cpy. | |
// (Compiler allowed to skip the cpy). | |
cout << endl; | |
/// Copy in assign (local) to ref? | |
// No copying when assigning to ref. | |
cout << " const MyObj& ref = x0:" << endl; | |
const MyObj& crx0 = x0; | |
cout << " MyObj& ref = x0:" << endl; | |
MyObj& rx0 = x0; | |
cout << endl; | |
/// Copy in assign result of fn to ref? | |
cout << " const MyObj& ref = f2():" << endl; | |
const MyObj& crx4 = f1(); // CONS (in f1()), no CPY. | |
// (Compiler allowed to skip the cpy). | |
outpC(crx4); | |
// Won't compile: | |
// invalid initialization | |
// of non-const reference of type ‘MyObj&’ | |
// from an rvalue of type ‘MyObj’ | |
// MyObj& xr4 = f1(); | |
cout << endl; | |
/// Are copy-ctor's invoked in function calls? | |
cout << " outp(MyObj):" << endl; | |
outp(x1); // CPY (and DEST) | |
cout << " outp(const MyObj&):" << endl; | |
outpC(x1); // no cpy | |
cout << endl; | |
/// Assignment | |
cout << " x2 = x3:" << endl; | |
x2 = x3; // ASSG, no cpy. | |
cout << endl; | |
/// Pointer | |
cout << " new MyObj(13):" << endl; | |
MyObj* ptr = new MyObj(13); // CONS | |
cout << " obj = *ptr:" << endl; | |
MyObj deref = *ptr; // CPY | |
cout << " obj& = *ptr:" << endl; | |
MyObj& derefRef = *ptr; // no cpy | |
cout << " const MyObj& = *ptr:" << endl; | |
const MyObj& constDeref = *ptr; // no cpy | |
cout << " delete ptr:" << endl; | |
delete ptr; // DEST | |
cout << endl; | |
cout << endl; | |
vectorStuff(); | |
cout << endl; | |
cout << "End of main()" << endl; | |
return 0; | |
} |
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
Beginning of main() | |
x0 = MyObj(10): | |
CONS Constructing MyObj(10) | |
x1(5): | |
CONS Constructing MyObj(5) | |
x2(x1): | |
CPY Copying from 5 | |
x3 = x1: | |
CPY Copying from 5 | |
x4 = f1(): | |
f1(): | |
CONS Constructing MyObj(7) | |
const MyObj& ref = x0: | |
MyObj& ref = x0: | |
const MyObj& ref = f2(): | |
f1(): | |
CONS Constructing MyObj(7) | |
Output MyObj(7) | |
outp(MyObj): | |
CPY Copying from 5 | |
Output MyObj(5) | |
DEST Destructing MyObj(5) | |
outp(const MyObj&): | |
Output MyObj(5) | |
x2 = x3: | |
ASSG Assigning from 5 to 5 | |
new MyObj(13): | |
CONS Constructing MyObj(13) | |
obj = *ptr: | |
CPY Copying from 13 | |
obj& = *ptr: | |
const MyObj& = *ptr: | |
delete ptr: | |
DEST Destructing MyObj(13) | |
vectorStuff: Begin | |
CONS Constructing MyObj(100) | |
vectorStuff: insert 1 | |
CPY Copying from 100 | |
vectorStuff: insert 2 | |
CONS Constructing MyObj(200) | |
CPY Copying from 200 | |
CPY Copying from 100 | |
DEST Destructing MyObj(100) | |
DEST Destructing MyObj(200) | |
vectorStuff: insert 3 | |
CONS Constructing MyObj(300) | |
CPY Copying from 300 | |
CPY Copying from 100 | |
CPY Copying from 200 | |
DEST Destructing MyObj(100) | |
DEST Destructing MyObj(200) | |
DEST Destructing MyObj(300) | |
vectorStuff: insert 4 | |
CONS Constructing MyObj(400) | |
CPY Copying from 400 | |
DEST Destructing MyObj(400) | |
vectorStuff: Done adding | |
vectorStuff: o1: | |
vectorStuff: o2: | |
CPY Copying from 200 | |
vectorStuff: o3: | |
vectorStuff: o4: | |
vectorStuff: Done mutating | |
Output MyObj(100) | |
Output MyObj(200) | |
Output MyObj(350) | |
Output MyObj(450) | |
vectorStuff: End | |
DEST Destructing MyObj(250) | |
DEST Destructing MyObj(150) | |
DEST Destructing MyObj(100) | |
DEST Destructing MyObj(200) | |
DEST Destructing MyObj(350) | |
DEST Destructing MyObj(450) | |
End of main() | |
DEST Destructing MyObj(13) | |
DEST Destructing MyObj(7) | |
DEST Destructing MyObj(7) | |
DEST Destructing MyObj(5) | |
DEST Destructing MyObj(5) | |
DEST Destructing MyObj(5) | |
DEST Destructing MyObj(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment