Created
August 29, 2020 19:52
-
-
Save mikeplavsky/7813be0e470e6e3606a4022e0844d6aa to your computer and use it in GitHub Desktop.
Returning object from function
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 <algorithm> | |
#include <iostream> | |
#include <sstream> | |
#include <iterator> | |
#include <vector> | |
#include <memory> | |
#include <forward_list> | |
#include <typeinfo> | |
using namespace std; | |
struct Next { | |
int a[23]; | |
}; | |
struct Tag { | |
string name = "Test"; | |
int * p = 0; | |
Tag() : p(new int[10]) { | |
cout << "C:" << this << " " << p << std::endl; | |
} | |
Tag & operator=(const Tag & t) { | |
cout << "E:" << t.p << std::endl; | |
return *this; | |
} | |
Tag(Tag && t) { | |
cout << "M:" << t.p << std::endl; | |
} | |
Tag(const Tag && t) { | |
cout << "M:" << t.p << std::endl; | |
} | |
~Tag() { | |
cout << "D:" << this << " " << p << std::endl; | |
delete [] p; | |
} | |
}; | |
void put(Tag && t) { | |
t.name = "Yes"; | |
cout << "P:" << t.p << " " << t.name << std::endl; | |
} | |
auto get() { | |
puts("get 1"); | |
Tag t; | |
puts("get 2"); | |
return t; | |
} | |
auto get1() { | |
Next n; | |
cout << "Next: " << n.a << std::endl; | |
return n; | |
} | |
Tag & get2() { | |
puts("get 2 - 1"); | |
Tag t; | |
puts("get 2 - 2"); | |
return t; | |
} | |
int main() | |
{ | |
puts("1"); | |
put(Tag()); | |
puts("2"); | |
Tag t = get(); | |
puts(t.name.c_str()); | |
puts("3"); | |
Tag t1; | |
t1 = t; | |
Next n = get1(); | |
cout << "Next: " << n.a << std::endl; | |
Tag & t2 = get2(); | |
puts("4"); | |
t2.name = "Y!"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment