Skip to content

Instantly share code, notes, and snippets.

@HungMingWu
Created December 4, 2012 02:42
Show Gist options
  • Save HungMingWu/4200056 to your computer and use it in GitHub Desktop.
Save HungMingWu/4200056 to your computer and use it in GitHub Desktop.
C++ Return Secret
#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;
}
#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