我做了以下简短的代码进行实验,并尝试获得"第一手"体验,当对象的构造函数和析构函数被调用时:
class Foo
{
public:
Foo(int bar)
{
this->bar = bar;
std::cout << "Standard constructor called" << std::endl;
}
~Foo()
{
std::cout << "Standard destructor called" << std::endl;
}
Foo(const Foo &foo)
{
std::cout << "Copy constructor called" << std::endl;
this->bar = foo.bar;
}
inline int get_bar() { return bar; }
private:
int bar;
};
Foo make_foo(int bar)
{
Foo f1(bar);
std::cout << "About to return foo with address of: " << &f1 << std::endl;
return f1;
}
int main() …Run Code Online (Sandbox Code Playgroud)