如果必须跨线程传递对象,哪种智能指针类型最好使用?
假设传递的对象是线程安全的.
我正在实施一个smartpointer模板,有一件事让我感到困惑; 将smartpointer作为参数传递给另一个函数时,如何增加引用计数器?我重载什么操作符来增加引用计数?
例如:
class test
{
test() { }
~test() { }
};
void A()
{
SmartPointer<test> p;
B(p);
}
void B(SmartPointer<test> p)
{
C(p);
}
void C(SmartPointer<test> p)
{
// p ref count is still 1 and will be destroyed at end of C
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个shared_ptrs的向量.我正在把auto_ptrs放进去.这没关系还是事情会破裂?
Room.hpp:
vector<shared_ptr<Item>> items;
void addItem(auto_ptr<Item>);
Run Code Online (Sandbox Code Playgroud)
主要:
room.addItem(auto_ptr<Item>(new Item(...)));
Run Code Online (Sandbox Code Playgroud) 我是C++的新手,我知道有三种返回局部变量的方法,并且都有它们的缺点:
Person& getPerson()
{
Person bob;
return bob;
}
Run Code Online (Sandbox Code Playgroud)
显然不是一个好主意.
Person getPerson()
{
Person bob;
return bob;
}
Run Code Online (Sandbox Code Playgroud)
没有空指针或悬空引用的可能性,但性能命中.
Person* getPerson()
{
return new Person();
}
Run Code Online (Sandbox Code Playgroud)
没有空指针的可能性,但肯定这违反了OO设计的基本规则.另一个对象将不得不删除 - 但为什么要这样做?getPerson()方法的实现与它无关.
所以,我正在寻找替代方案.我听说过共享指针和智能指针(标准和Boost),但我不确定它们中是否有任何一个用于处理这个问题.你们有什么建议?
我使用OpenCv,我在代码中使用它类似于以下代码:
Mat Create()
{
Mat myMat(10, 10, CV8U_C1);
int x=myMat.Rows; // I am accessing Mat like an object not a pointer.
Return myMat;
}
Main()
{
Mat aMat=Create(); // created inside this function
int x=aMat.Rows; // accessing it using . notation
// do some work
return; //I did not delete Mat, as it would release its memory.
}
Run Code Online (Sandbox Code Playgroud)
如何在我的c ++代码中创建类似的对象?
我正在使用STL,但如果需要我也可以使用Boost.
我正在为类创建一个函数,并且参数被声明为void*但是在我需要测试的函数中,如果这个void*是shared_ptr还是unique_ptr,是否有办法测试这种情况?
这就是我到目前为止所做的工作; 我的类是模板类型,不存储任何成员变量.它有一个默认的构造函数,它也可以通过传入一个shared_ptr<Type>或一个来构造,unique_ptr<Type>并且它具有多个allocate()函数,它们执行相同类型的工作.
#ifndef ALLOCATOR_H
#define ALLOCATOR_H
#include <memory>
#include <iostream>
template<class Type>
class Allocator {
public:
Allocator(){}
Allocator( Type type, void* pPtr );
Allocator( std::shared_ptr<Type>& pType );
Allocator( std::unique_ptr<Type>& pType );
// ~Allocator(); // Default Okay
void allocate( std::shared_ptr<Type>& pType );
void allocate( std::unique_ptr<Type>& pType );
void allocate( Type type, void* pPtr );
private:
Allocator( const Allocator& c ); // Not Implemented
Allocator& operator=( const Allocator& c ); // Not Implemented
}; // …Run Code Online (Sandbox Code Playgroud) 我有一个customClass1有一个属性的类std::vector<std::shared_ptr<customClass2>>.
如何制作一个customClass1对象的副本,该对象包含指向第一个元素所指向的对象的相同副本的指针std::vector<std::shared_ptr<customClass2>>?
我并不想简单地使包含于载体指针的副本.我想实际制作指针指向的对象的副本,然后指向存储在我的第二个customClass1对象的vector属性中的这些新对象.
让我们p成为共享/唯一指针.是if (p)与if (p.get())相同呢?
如果不是,这些条件或条件中的代码在什么情况下表现不同?
从cppreference我读到std::shared_ptr::operator bool检查是否get() != nullptr.这是确切的实施operator bool吗?
我编写了一个小程序,以检查异常情况下创建shared_ptrvia new和make_shared()函数之间的区别。我到处都读到,通过make_shared()它是异常安全的。
但是,这两种情况的有趣之处在于,两种情况下的析构函数都不会在堆栈展开后调用?我错过了什么吗?提前致谢。
#include <iostream>
#include <memory>
class Car
{
public:
Car() { cout << "Car constructor!" << endl; throw std::runtime_error("Oops"); }
~Car() { cout << "Car destructor!" << endl; }
};
void doProcessing()
{
// std::shared_ptr<Car> sp(new Car());
std::shared_ptr<Car> sp2 = std::make_shared<Car>();
}
int main()
{
try
{
doProcessing();
}
catch(...)
{
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个与智能指针和原始指针有关的问题。
我的第一个想法是使用原始指针:因此,如果在一个类(例如Routes类)中,其属性是map<string, list<Route *>> _mapIATA和map<int, list<Route*>> _mapID,那么我将必须在该类中实现一个destroyer,一个副本和一个operator =方法,我错了吗?
但是,如果我不是使用原始指针,而是使用智能指针,则不必担心删除指向的内容,但是复制和分配呢?
但目前,我不确定会更好。原始或智能指针。
谢谢!
c++ ×10
smart-pointers ×10
shared-ptr ×3
boost ×2
c++11 ×2
pointers ×2
copy ×1
dictionary ×1
exception ×1
opencv ×1
reference ×1
stl ×1
unique-ptr ×1
vector ×1