make-shared

    3熱度

    2回答

    正如在Difference in make_shared and normal shared_ptr in C++的回答中所指出的,在大多數情況下,make_shared勝過shared_ptr。 那麼爲什麼C++標準將shared_ptr和make_shared都定義在一起?有什麼情況下我應該更喜歡shared_ptr到make_shared甚至在哪個我只能使用shared_ptr而不是make

    3熱度

    2回答

    我使用std::weak_ptr和std::make_shared時偶然發現這種行爲,我覺得這一點都不奇怪。我正在使用C++ 11。 #include <iostream> #include <memory> int main() { std::weak_ptr<int> weak; std::shared_ptr<int> shared {std::make_sha

    0熱度

    1回答

    我是C++新手。有人可以請讓我知道什麼是錯用下面的代碼段 - class Person { public: const std::string& name; Person(const std::string& s): name(s) {} void dump(void) const { cout << name << endl;

    1熱度

    1回答

    我必須通過boost::shared_ptr: boost::shared_ptr<Protobuf::Person::Profile> pProfile = boost::make_shared<Protobuf::Person::Profile>(); 是的protobuf的指針,在protobuf的函數oPerson.set_allocated_profile(pProfil

    0熱度

    1回答

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

    1熱度

    2回答

    初始化const成員下面的代碼編譯使用的Xcode 6.3.2但不是的Visual Studio 2013年 #include <cstdint> #include <memory> class Y { public: Y(uint32_t i) : m_i(i) { } private: uint32_t m_i; }; class X

    4熱度

    2回答

    在this回答T.C.狀態 boost::make_shared等支持數組類型 - 無論是未知 大小中的一個,或固定大小 boost::shared_ptr<int[]> sh_arr2 = boost::make_shared<int[]>(30); boost::shared_ptr<int[30]> sh_arr3 = boost::make_shared<int[30]>(); 首先

    3熱度

    2回答

    是什麼 auto sp = std::make_shared<Foo>(); auto sp(std::make_shared<Foo>()); 需要詳細解釋之間的差異。

    2熱度

    2回答

    我想傳遞一個指向一個函數的堆棧變量(我不控制),只需要一個boost::shared_ptr。 根據this answer,使用boost::make_shared是要走的路。爲了測試這個功能,我寫了這個: #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> int mai

    9熱度

    1回答

    據我所知make_shared<T>(...)可能會提供一些內存分配優化(它可能會分配參考計數器在同一個內存塊中作爲類T的實例)。 enable_shared_from_this是否提供了相同的優化?所以: class T : std::enable_shared_from_this<T> {}; ... auto t = std::shared_ptr<T>(new T); 是一樣的: