unique-ptr

    0熱度

    2回答

    我有一個原始指針,並希望回調擁有它。我拿出這個代碼: A* a = new A(); dosomething(callback(unique_ptr<A>(a).get())) // The callback always takes a raw pointer. void callback(A* a) { } 這是正確的方法嗎?

    0熱度

    3回答

    class foo { foo(); unique_ptr<T, (void *)(T*)> ptr; }; foo::foo() { bool x = some operation; if (!x) throw; ptr = unique_ptr<T, (void *)(T*)>(new T(x), deleter); } 此代碼不能

    1熱度

    1回答

    考慮與不能直接存儲的成員,例如一類,因爲它不會有一個默認的構造函數,封裝類的構造函數沒有足夠的信息來創建它: class Foo { public: Foo(){} // Default ctor private: /* Won't build: no default ctor or way to call it's non-default ctor at F

    2熱度

    2回答

    此代碼編譯成功。 #include<iostream> #include<memory> using namespace std; class A{ public: unique_ptr<A> myval; A(){ cout<<"Constrcutor of A is called"<<endl; } ~A(){cout<<"Destruct

    0熱度

    1回答

    下面的代碼不會鏗鏘-700.1.81編譯和它的標準庫: ......./include/c++/v1/memory:2626:46: note: in instantiation of member function 'std::__1::unique_ptr.....requested here _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();

    1熱度

    2回答

    我有一個(例如)uint8_t s的數組。 是std::unique_ptr錯誤的工具用來管理內存中的這個對象? 例如, std::unique_ptr<uint8_t> data(new uint8_t[100]); 這是否會產生不確定的行爲? 我想要一個智能指針對象來爲我管理一些分配的內存。 std::vector並不理想,因爲它是一個動態對象。 std::array也不好,因爲分配的大小

    0熱度

    2回答

    我有一個std::vector<std::unique_ptr<T>> vec1其中T是一個抽象類型。我想創建std::vector<T*> vec2,其中來自第二個向量的指針指向的對象是由第一個向量指針指向的對象的副本。 因此,例如:*(vec1[0]) == *(vec2[0])和vec1[0].get() != vec2[0] ...等... 如何做到這一點?

    4熱度

    2回答

    據我所知,unique_ptr是某個對象的單一所有者,當它超出範圍時,它會釋放該對象。我不明白的是以下情況: unique_ptr<int> p(new int(1)); p = unique_ptr<int>(new int(2)); new int(1)如果p被重定向到另一個存儲位置new int(2)(因爲p只能擁有其中的一個)與第一對象會發生什麼?

    -1熱度

    1回答

    我有一個關於超載<<運算符的問題。 我有一個名爲House類中,我已經定義如下(我沒有宣佈它作爲friend,這是一個免費的「功能」,因爲我不打算訪問私有成員): std::ostream& operator<<(std::ostream& stream, House const& house) { // Append strings to 'stream' return s

    0熱度

    1回答

    我正在建造一個工廠類,我需要將unique_ptr返回到BaseClass。返回指針是由使用make_shared被轉換到共享指針DerivedClass對象,然後轉化爲所需BaseClass指針爲: #include "BaseClass.h" #include "DerivedClass.h" std::unique_ptr<BaseClass> WorkerClass::DoSomet