存储的临时对象在哪里
Where is the temporary object stored?
我想知道调用const reference的函数时,临时对象在哪里存储。
在下面的方案中,在 foo 函数中,我们创建一个临时 std :: string 对象,然后将其作为参数传递给 print_name 能。获取const引用。它是否存储在堆上的某个地方,而不是在 print_name 函数范围内被销毁。
谢谢
void print_name(const std::string& name)
{
std::cout << name << std::endl;
}
void foo()
{
print_name( std::string("John") );
}
它存储在堆栈上,而不是堆中。但是请注意,std::string
可能利用了堆。
相关文章:
- 在不复制临时对象的情况下延长其生存期
- 是否可以在C++中移动临时对象的属性?
- 如何在没有 std::move 的情况下移动临时对象
- 堆分配对象中的堆栈对象在 c++ 中在哪里分配?
- 临时对象在C++中是不可避免的吗?
- 为什么在按值返回时创建临时对象,而不是在按值传递给函数参数时创建临时对象
- 当线程处理不同的类时,应该在哪里声明条件变量、互斥对象
- 为什么引用类型在使用临时对象访问时是左值
- MyClass obj = MyClass();'MyClass()'在这里指的是临时对象吗?
- 组件对象模型 (COM):IMalloc::Alloc 在哪里分配内存?
- 当我们在C++中创建类的对象时,为成员函数分配的内存在哪里?
- 工厂方法创建的对象应该在哪里删除?
- 在函数调用中C++临时对象的生存期
- 在锁定下清除STD ::映射,而移动到临时对象
- 在基本的GUI编程中,在哪里声明对象和方法
- 存储的临时对象在哪里
- 为什么在传递临时对象时不调用复制构造函数
- 临时对象存储在哪里
- 为什么在返回临时对象之前调用move构造函数?
- 在哪里删除对象?我超出了范围