小编Ano*_*son的帖子

堆栈上的C++变量似乎不会在函数范围结束时释放

我做了以下简短的代码进行实验,并尝试获得"第一手"体验,当对象的构造函数和析构函数被调用时:

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)

c++ scope

1
推荐指数
1
解决办法
58
查看次数

标签 统计

c++ ×1

scope ×1