#include <iostream>
#include <string>
class Test {
public:
static unsigned int _id;
Test(): _a(1000) {
_effective_id = _own_id = _id++;
log("Default constructor");
};
Test(int a): _a(a) {
_effective_id = _own_id = _id++;
log("Int constructor with value " + std::to_string(a));
};
Test(const Test& other) {
_a = other._a;
_effective_id = _own_id = _id++;
log("Copy constructor with id " + std::to_string(other._own_id));
}
Test(Test&& other) {
_a = other._a;
_effective_id = _id++;
_own_id = other._own_id;
log("Move constructor with id " + std::to_string(other._own_id));
other._move_made = true;
}
~Test() {
log("Destructor");
if(_move_made) {
log("After move");
}
}
Test& operator=(const Test& other) {
_a = other._a;
log("Copy operator with id " + std::to_string(other._own_id));
return *this;
}
Test& operator=(Test&& other) {
_a = other._a;
other._move_made = true;
_effective_id = _own_id;
_own_id = other._own_id;
log("Move copy operator with id " + std::to_string(other._own_id));
return *this;
}
private:
int _a;
unsigned int _own_id, _effective_id;
bool _move_made = false;
void log(std::string msg) {
std::cout << _own_id << "(effective: " << _effective_id << ")" << ": " + msg + "n";
}
};
Test get_test() {
std::cout << "Inside get_test()n";
return Test(5);
}
Test&& get_move() {
std:: cout << "Inside get_move()n";
return std::move(Test(7));
}
void copy_pass(Test copy) {
std::cout << "Took by copyn";
}
void pass(const Test& ref) {
std::cout << "Took by refn";
}
void pass(Test&& subject) {
std::cout << "Took by rvaln";
}
unsigned int Test::_id = 0;
int main() {
std::cout << "Initialize an";
Test a = get_test();
std::cout << "Pass by copyn";
copy_pass(a);
std::cout << "Pass by refn";
pass(a);
std::cout << "Pass by moven";
pass(std::move(a));
std::cout << "Initializing bn";
Test b(7);
std::cout << "Move to cn";
auto c = std::move(b);
std::cout << "Ref to dn";
auto& d = c;
std::cout << "Copy by ref to en";
Test e(d);
std::cout << "Copy by rval to fn";
Test f(std::move(e));
std::cout << "Getting by moven";
Test g(get_move());
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}