You can construct a std::shared_ptr<T>
(or a std::shared_ptr<T[]>
) from a T*
. If the T
(s) weren't allocated with new
([]
), you will need to provide the correct deallocation to the shared pointer.
Assuming that there is a void returnPointer(T* pT, int size);
in that C interface.
struct C_interface_deleter {
void operator()(T* ptr) { returnPointer(ptr, size); }
int size;
};
void use_C_interface() {
T* raw;
int size;
getPointer(&raw, &size);
std::shared_ptr<T> ptr(raw, C_interface_deleter{ size });
// stuff involving ptr
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…