标签: smart-pointers

提升智能指针和线程

如果必须跨线程传递对象,哪种智能指针类型最好使用?

假设传递的对象是线程安全的.

c++ multithreading smart-pointers

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

将智能指针作为参数传递给函数

我正在实施一个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)

谢谢

c++ memory-management smart-pointers reference-counting

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

C++ - 将auto_ptrs放在shared_ptr向量中

我有一个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++ memory-management smart-pointers

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

在C++中返回局部变量的引用和标准指针有哪些替代方法?

我是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),但我不确定它们中是否有任何一个用于处理这个问题.你们有什么建议?

c++ boost pointers reference smart-pointers

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

什么类型的对象是opencv :: Mat?它是shared_ptr还是auto_ptr?它是指针吗?

我使用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.

c++ boost opencv stl smart-pointers

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

测试传递给函数的void*是shared_ptr还是unique_ptr

我正在为类创建一个函数,并且参数被声明为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)

c++ smart-pointers void-pointers reinterpret-cast

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

创建shared_pointers向量元素指向的对象副本

我有一个customClass1有一个属性的类std::vector<std::shared_ptr<customClass2>>.

如何制作一个customClass1对象的副本,该对象包含指向第一个元素所指向的对象的相同副本的指针std::vector<std::shared_ptr<customClass2>>

并不想简单地使包含于载体指针的副本.我想实际制作指针指向的对象的副本,然后指向存储在我的第二个customClass1对象的vector属性中的这些新对象.

c++ smart-pointers vector shared-ptr c++11

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

智能指针作为条件:是(p)和if(p.get())equilavent?

让我们p成为共享/唯一指针.是if (p)if (p.get())相同呢?

如果不是,这些条件或条件中的代码在什么情况下表现不同?

cppreference我读到std::shared_ptr::operator bool检查是否get() != nullptr.这是确切的实施operator bool吗?

c++ smart-pointers shared-ptr unique-ptr c++11

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

展开堆栈后,不会删除智能指针中的对象

我编写了一个小程序,以检查异常情况下创建shared_ptrvia newmake_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)

c++ exception smart-pointers shared-ptr

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

C ++:在原始指针的映射中复制,删除和运算符=

我有一个与智能指针和原始指针有关的问题。

我的第一个想法是使用原始指针:因此,如果在一个类(例如Routes类)中,其属性是map<string, list<Route *>> _mapIATAmap<int, list<Route*>> _mapID,那么我将必须在该类中实现一个destroyer,一个副本和一个operator =方法,我错了吗?

但是,如果我不是使用原始指针,而是使用智能指针,则不必担心删除指向的内容,但是复制和分配呢?

但目前,我不确定会更好。原始或智能指针。

谢谢!

c++ dictionary pointers copy smart-pointers

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