unique-ptr

    -2熱度

    1回答

    目前我有這樣的代碼在C++(我使用Visual Studio 2013): char * dest= new char[srcLen + 1] {}; strcpy(dest, source); std::string s(dest); delete dest; 如何將它轉換爲一個C++ 11 unique_ptr使用make_unique以便它可以使用strcpy()? 我想: aut

    4熱度

    1回答

    如果我按如下方式將unique_ptr指向STL容器的實例,該怎麼辦?這段代碼安全嗎? unique_ptr< vector<int> > p1(new vector<int>); 這是不是導致的析構函數vector<int>被調用兩次,因爲兩者的vector<int>本身和unique_ptr都試圖清理內存的vector<int>迄今取得的?這可能導致未定義的行爲?或者編譯器以某種方式知道v

    2熱度

    1回答

    我有這三個功能 unique_ptr<Object> Holder::remove(string objName){ std::vector<unique_ptr<Object>>::iterator object = find_if(objects.begin(), objects.end(), [&](unique_ptr<Object> & obj){

    2熱度

    1回答

    移動由unique_ptr指向的對象而不是uptr本身是合法的嗎?例如std::unique_ptr<Foo> uptr(new Foo()); Foo bar = std::move(*uptr);任何潛在的問題? std::move語義狀態,在移動時,對象處於有效但未指定的狀態。 unique_ptrreset/release仍然安全嗎? 我想這對向量和它的作品 - https://ideon

    1熱度

    1回答

    這是很常見的前置聲明,以避免循環的頭文件的依賴性,或者實現PIMPL,那麼人們可能會用類似的代碼結束: class A; class B { A * a; }; 當一個人試圖用的unique_ptr取代這個: class A; class B { std::unique_ptr<A> a; }; 編譯器抱怨。什麼是最好的做法,讓這個工作?

    1熱度

    1回答

    在我的代碼 std::unique_ptr<QNetworkAccessManager> myNetworkAccessManager; ... myNetworkAccessManager.reset(new QNetworkAccessManager(this)); QObject::connect(myNetworkAccessManager.get(), SIGNAL(finish

    1熱度

    2回答

    因此,我構建了一個模擬器以模擬一些負載平衡算法。我已經創建磁芯和調度員的2個向量,如下所示: std::vector<std::unique_ptr<Dispatcher> > vDisp; std::vector<std::unique_ptr<Core> > vCore; 類調​​度器具有用於核心的隊列,以便分配給它們的作業。 std::queue<Core> disp_core_que

    3熱度

    1回答

    將容器(例如:std::vector)傳遞給函數模板時,是否可以從容器中抽象出對象的指針類型? 我有以下兩種方法: template <typename T, typename allocT, template <typename, typename> class containerT> static void parse(containerT<T *, allocT> &entries, co

    3熱度

    1回答

    我有,需要一個指針的指針,並填補了值的函數: void GetSensor(Sensor **sensor); 通常情況下,我必須這樣做以創建傳感器和無它: Sensor *sensor; GetSensor(&sensor); // Do something with the sensor delete sensor; 現在我想爲同一個任務使用std :: unique_ptr。我

    0熱度

    1回答

    有人可以在這裏解釋崩潰嗎? #include <iostream> #include <memory> int main() { int *num1 = new int(5), *num2 = new int(18); std::unique_ptr<int> numptr = std::unique_ptr<int>(num1); std::cout << *