复制构造函数、析构函数和临时变量
copy constructor,destructor and temporaries
我编写这个类是为了测试默认构造函数、复制构造函数、赋值运算符和析构函数的行为:
#include <iostream>
class Test {
public:
Test();
Test(const Test&);
~Test();
Test &operator=(const Test&);
private:
static int count;
int label;
};
Test::Test() : label(count++)
{
std::cout<<"constructor of "<<label<<std::endl;
}
Test::Test(const Test &other) : label(count++)
{
std::cout<<"copy-constructor of "<<label<<std::endl;
}
Test::~Test()
{
std::cout<<"destructor of "<<label<<std::endl;
}
Test &Test::operator=(const Test &other)
{
std::cout<<"copy assignment operator of "<<label<<std::endl;
}
int Test::count=0;
我在不同的上下文中使用了这个类,以深入了解如何以及何时调用每个函数:我期望评论中的行为:
#include "Test.h"
// argument passed and returned by reference
const Test &funct_by_ref(const Test &ref)
{
return ref;
}
// argument passed and returned by value
// call copy constructor to initialize parameter
Test funct_by_val(Test val)
{
// calls copy constructor to initialize temporary
return val;
} // exits local scope,val is destroyed,calls val destructor
int main()
{
// creates a temporary,calls 0 default constructor
Test(); // the temporary is destroyed at the end of the expression that created it
// calls 0 destructor
// creates a temporary (calls 1 default constructor) and calls 2 copy constructor
Test t2=Test(); // same as Test t2((Test()));
std::cout<<"--------------------------------"<<std::endl;
// calls 3 copy constructor
Test t3=t2;
// calls 4 default constructor
Test t4;
{
// calls 5 copy constructor
Test t5(t4);
} // local scope,t5 is destroyed,calls 5 destructor
// calls 4 assignment operator
t4=t2;
std::cout<<"-------------------------------"<<std::endl;
// nothing happens here
funct_by_ref(t4);
std::cout<<"-------------------------------"<<std::endl;
// calls copy constructor twice 6,7
funct_by_val(t4);
// temporary is destroyed at the end of the expression,calls destructor
}
但我得到了以下输出:
constructor of 0
destructor of 0
constructor of 1
------------------------
copy-constructor of 2
constructor of 3
copy-constructor of 4
destructor of 4
copy assignment operator of 3
--------------------------
---------------------------
copy-constructor of 5
copy-constructor of 6
destructor of 6
destructor of 5
destructor of 3
destructor of 2
destructor of 1
一切都很好,直到第一个----------------它似乎跳过了对象的创建(我想到了初始化t2的临时用法,因为它没有在那一行之后被破坏(,所以计数减少了一。。
Test t2=Test(); // same as Test t2((Test()));
在这里,编译器正在(并且可以(删除副本,并且将具有与执行以下操作相同的效果:
Test t2;
如果检测临时库存在的唯一方法是跟踪构造函数和析构函数调用,则该标准有特定的规定允许但不要求实现消除(即省略创建(临时库。
你看到的差异是因为你预期的行为是基于临时性的创建和销毁,而编译器选择不创建这些临时性。请注意,取消临时性的决定各不相同,这取决于实施(通常受优化设置的影响(。
相关文章:
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 使用线程销毁类析构函数中的shared_ptr成员变量
- 为什么不调用全局静态变量的析构函数
- cpp 中的析构函数是否自动调用?即使析构函数没有提及非动态变量,它们也会被删除吗?
- 返回堆栈变量时停止调用析构函数
- 如何使用非平凡析构函数防止未使用的变量警告
- 如何释放C++异常类析构函数中的变量
- 为什么msvc编译器在显式调用析构函数时会发出未使用的变量
- 当变量仍然存在时调用C++析构函数
- 为静态分配的变量手动调用析构函数
- 人造人.NDK.如何记录全局变量的调用析构函数
- 在析构函数调用后访问成员变量
- 为什么析构函数不修改返回的变量
- 从静态变量引用非静态变量会使非静态变量先于静态变量进行析构函数
- 在C++中,析构函数的调用顺序和成员变量的销毁顺序是什么
- 全局变量析构函数中的 ctrl-c 上的 AV
- 函数静态变量析构函数和线程
- Boost变量析构函数导致分段错误
- 在库析构函数之前调用静态变量析构函数
- C++类变量析构函数