我有一個原始指針,並希望回調擁有它。我拿出這個代碼: A* a = new A();
dosomething(callback(unique_ptr<A>(a).get()))
// The callback always takes a raw pointer.
void callback(A* a) {
}
這是正確的方法嗎?
考慮與不能直接存儲的成員,例如一類,因爲它不會有一個默認的構造函數,封裝類的構造函數沒有足夠的信息來創建它: class Foo
{
public:
Foo(){} // Default ctor
private:
/* Won't build: no default ctor or way to call it's
non-default ctor at F
此代碼編譯成功。 #include<iostream>
#include<memory>
using namespace std;
class A{
public:
unique_ptr<A> myval;
A(){ cout<<"Constrcutor of A is called"<<endl; }
~A(){cout<<"Destruct
下面的代碼不會鏗鏘-700.1.81編譯和它的標準庫: ......./include/c++/v1/memory:2626:46: note: in instantiation of member function 'std::__1::unique_ptr.....requested here
_LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();