unique-ptr

    0熱度

    1回答

    我知道還有其他方法可以做到這一點。但我決定嘗試一下: unique_ptr<int> pi; pi = std::move(new int); 當我編譯這段代碼時,編譯器給出了一個錯誤。爲什麼?

    0熱度

    2回答

    當您將一個unique_ptr複製到另一個時,正在閱讀有關智能指針的更多信息,並遇到了構造函數被刪除的概念。這個概念到底是什麼? #include<iostream> #include<memory> class Person { public: int e; Person(int e) : e(e) { } }; int main() { st

    0熱度

    2回答

    我有以下程序 - #include <iostream> #include <memory> class Person { public: Person(const std::string& name): name(name) { } ~Person() { std::cout << "Destroyed" << std::endl; }

    -1熱度

    1回答

    class A{ public: A():p(nullptr){}; private: std::unique_ptr<B> p; // B is some class }; 當A的對象超出範圍時,將自動刪除佔用內存空間的內存空間,該內存空間消耗的內存空間爲p。 我們是否應該像下面那樣顯式編寫析構函數? ~A(){ delete p;

    0熱度

    1回答

    我在努力正確初始化的std::unique_ptr's。 示例代碼: #include <iostream> #include <vector> #include <memory> class Base{ public: std::string getString() { return this->string; }; protected: st

    2熱度

    2回答

    我的目標就是在這裏實現的unique_ptr的簡單版本,只提供一個構造函數,析構函數 - >,*和釋放()。 不過,我不知道該怎麼在一個的unique_ptr使用未分配的指針初始化的情況下做的。 如 int i = 0; unique_ptr<int> p{&i}; 如果的unique_ptr只是調用刪除它擁有的指針,這將產生不確定的(和不受歡迎的)行爲,至少據我所知。我能做些什麼來防止這種

    0熱度

    2回答

    myMain.cpp初始化: #include <memory> #include "myClass.h" static std::unique_ptr<myClass> classPtr; // Error thrown here ... 我在全局範圍內初始化,因爲所有的數據加載到這個類的屬性需要一段時間,所以我想這樣做一次,並有該數據一直存在,直到我給出一個明確的命令來刪除它(cl

    -2熱度

    1回答

    對於我的項目,我有這樣的階級結構: class Base { enum class type { Derived1, Derived2 } int getType() {return type;}; type type; //general methods & attributes } class

    2熱度

    2回答

    我讀這個答案霍華德Hinnant(Is std::unique_ptr<T> required to know the full definition of T?),然後這個答案(How is a template instantiated?),我只是在想。如果你有像這樣 class Something { Something(); ~Something(); cla

    10熱度

    1回答

    我們最近在使用自定義刪除器從unique_ptr移動到shared_ptr時遇到了崩潰。當用於創建智能指針的指針爲空時發生崩潰。下面是重現問題的代碼,並顯示了兩種可行的案例。 在下面的源代碼中,One和Two愉快地運行,而在「ReleaseDestroy」中發生三次崩潰。當智能指針中使用的類具有虛擬「發佈」並且程序嘗試查找V-表時,崩潰似乎正在發生。 unique_ptr看起來像它檢查空指針並且不