unique-ptr

    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

    0熱度

    2回答

    我花了一些時間試圖追蹤這個,但我在這裏有一個小例子,它顯示了我所看到的錯誤。如果我忽略重置行,它工作得很好。 #include <memory> #include <unordered_map> #include <iostream> class Base { public: virtual ~Base() = 0; }; Base::~Base(){} c

    1熱度

    1回答

    我收到一個奇怪的錯誤。當我試圖返回一個唯一指針到一個派生類型的函數中,該函數返回一個指向基類型的唯一指針,就像返回值被降級爲基類型一樣。我無法訪問它的任何方法或成員,並且如果我嘗試,程序會出現段錯誤。 如果我嘗試將e1重新分配給返回類型並調用它的「eval」方法,下面的示例會出現segfaults。奇怪的是,在返回之前調用eval方法正常工作。當我使用賦值變量作爲函數的參數之一時,似乎發生這種情況

    3熱度

    1回答

    我有一個是這樣的代碼: #include <memory> #include <vector> using namespace std; struct A { virtual ~A() = default; }; struct B : public A { }; template<typename... Ts> struct C { C() : v_({n

    2熱度

    2回答

    我試圖創建一個簡單的結構體來保存對其父項的某些值的引用。父存儲在向量內的unique_ptr內。它在被移動之前被實例化。運動後,參考課程不再有效。我找到了重新實現它們的方法,但我討厭解決方案(如下所示)。我認爲移動構造函數在collection.push_back(std::move(d))上被調用,但是它不是Derived的情況。雖然這可能是unique_ptr,但我不確定。 我的問題是 - 什

    -3熱度

    2回答

    我剛剛穩定的情況是一個unique_pointer引用作爲函數參數傳遞。所以我看了一下,發現代碼實際上是在編譯和運行。 這是爲什麼呢? 當你可以引用該指針時,指針是如何唯一的? 這裏我舉的例子: class Foo{ public: int bar{23}; }; void Bar(std::unique_ptr<Foo>& a_foo){ a_foo->bar