Created
December 4, 2012 02:42
-
-
Save HungMingWu/4200056 to your computer and use it in GitHub Desktop.
C++ Return Secret
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> | |
struct foo | |
{ | |
static int count; | |
int id; | |
foo() : id(++count) | |
{ | |
std::cout << "Constructing! " << id << std::endl; | |
} | |
foo(const foo& f) :id(++count) | |
{ | |
std::cout << "Copy constructing! " << id << std::endl; | |
} | |
~foo() | |
{ | |
std::cout << "Destructing.. " << id << std::endl; | |
} | |
}; | |
int foo::count = 0; | |
foo get() | |
{ | |
foo f; | |
return f; | |
} | |
int main() | |
{ | |
foo f = get(); | |
std::cout << "before return" << std::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
#include <iostream> | |
struct foo | |
{ | |
static int count; | |
int id; | |
foo() : id(++count) | |
{ | |
std::cout << "Constructing! " << id << std::endl; | |
} | |
foo(const foo& f) :id(++count) | |
{ | |
std::cout << "Copy constructing! " << id << std::endl; | |
} | |
~foo() | |
{ | |
std::cout << "Destructing.. " << id << std::endl; | |
} | |
}; | |
int foo::count = 0; | |
foo get() | |
{ | |
foo f; | |
return f; | |
} | |
int main() | |
{ | |
const foo& f = get(); | |
std::cout << "before return" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment