unique-ptr

    1熱度

    2回答

    我有問題在unique_ptr中間的const_iterator上使用std :: rotate。 我曾嘗試: std::vector<std::unique_ptr<Object> >::const_iterator middle; // middle is pointing at somewhere within vec defined elsewhere. std::rotate(vec

    4熱度

    1回答

    我一直在使用原始指針進行依賴注入,並決定將我的代碼轉換爲使用shared_ptr。這工作,但我想知道我是否可以使用unique_ptr?在我的例子中,MyClass將管理信用卡服務的生命週期。 class PaymentProcessor { PaymentProcessor(?? creditCardService): :creditCardService_(credit

    7熱度

    4回答

    使用unique_ptr創建對象並賦予容器所有權是沒有問題的。如何通過原始指針去除元素? std::set<std::unique_ptr<MyClass>> mySet; MyClass *myClass = new MyClass(); mySet.insert(std::unique_ptr<MyClass>(myClass)); // remove myClass from my

    1熱度

    2回答

    所以我有std::vector<std::unique_ptr<Base>> vec,我試圖動態地對它進行排序,因爲Derived1與Derivedn(Derivedn總是> Derivedn-1> ...> Derived1)(比如n = 10左右)之間存在邏輯比較每個Derivedx都有自己與Derivedx的不同比較。作爲一個例子,認爲10位整數> 9位數整數> 1位整數,但在每個派生類53

    0熱度

    1回答

    我從Visual c得到warning C4355: 'this' : used in base member initializer list ++ 2010: 我有一個類拿着手柄,我想自動關閉句柄即使ctor爲類失敗(所以它的dtor不叫)。但是,我不想費心製作一個整體的句柄包裝類,而寧願將它放在一個智能指針中。所以我寫了這個: foo.h ~~~~~ class Foo {

    1熱度

    3回答

    在C++ 11中,我缺少一個用於將指針分解爲std::unique_ptr的合成糖。因此,我寫了以下小幫手函數std::uniquify_ptr,通常用於易變(非構造函數)賦值的可變類成員(通常是不同種類的緩存)。 #include <memory> namespace std { template<typename T> inline unique_ptr<T> uni

    30熱度

    5回答

    我曾嘗試以下: std::function<void()> getAction(std::unique_ptr<MyClass> &&psomething){ //The caller given ownership of psomething return [psomething](){ psomething->do_some_thing(); //p

    20熱度

    2回答

    如何在不將容器的所有權從容器中取出的情況下訪問容器的unique_ptr元素(通過迭代器)?當一個迭代器獲得容器中的元素時,容器的元素所有權仍然存在?如何取消引用迭代器以訪問unique_ptr?這是否執行unique_ptr的隱式移動? 我發現我使用shared_ptr的很多時候我需要存儲元素的容器(不是值),即使容器在概念上擁有的元素和其他代碼只是希望操縱元素的容器,因爲我害怕不能實際訪問容器

    55熱度

    4回答

    根據N3290 std::unique_ptr在其構造函數中接受deleter參數。 但是,我不能在Windows中使用Visual C++ 10.0或MinGW g ++ 4.4.1,也不能在Ubuntu中使用g ++ 4.6.1。 因此,我害怕我對它的理解是不完整或錯誤的,我無法看到明顯被忽略的刪除參數,所以任何人都可以提供一個工作示例? 最好我想看看它是如何工作的unique_ptr<Bas

    0熱度

    1回答

    在運行時關閉我的程序時出現錯誤:「crt檢測到應用程序在堆緩衝區結束後寫入內存。」我通過析構函數將程序執行過程跟蹤到唯一ptr的刪除器,並在調用deleter函數時發生了錯誤。我想提到的另一個問題是,由於某些原因,唯一ptr所在的類defenition要求它具有公共拷貝構造函數。我嘗試在另一個具有不同類和相同項目的項目中複製這些問題,刪除複製構造函數和析構函數並更改訪問實驗室。我無法複製相同的問題