正如您所知,由於包含類的shared_pointer尚未存在,因此無法使用來自對象構造函數的std :: enable_shared_from_this和shared_from_this()對。但是,我真的很想要這個功能。我嘗試了我自己的系統,它似乎工作正常。在構造函數中使用shared_from_this()
namespace kp
{
template <class T>
void construct_deleter(T *t)
{
if(!t->_construct_pself)
{
t->~T();
}
free(t);
}
template <class T, typename... Params>
std::shared_ptr<T> make_shared(Params&&... args)
{
std::shared_ptr<T> rtn;
T *t = (T *)calloc(1, sizeof(T));
t->_construct_pself = &rtn;
rtn.reset(t, construct_deleter<T>);
t = new(t) T(std::forward<Params>(args)...);
t->_construct_pself = NULL;
t->_construct_self = rtn;
return rtn;
}
template <class T>
class enable_shared_from_this
{
public:
std::shared_ptr<T> *_construct_pself;
std::weak_ptr<T> _construct_self;
std::shared_ptr<T> shared_from_this()
{
if(_construct_pself)
{
return *_construct_pself;
}
else
{
return _construct_self.lock();
}
}
};
}
任何人都可以發現這個邏輯中的任何缺陷?我基本上使用placement new來在構造函數調用之前爲該類內的shared_ptr指定一個指針。
既然這樣我可以用它作爲這樣:
std::shared_ptr<Employee> emp = kp::make_shared<Employee>("Karsten", 30);
,並在Employee構造:
Employee::Employee(std::string name, int age)
{
Dept::addEmployee(shared_from_this());
}
我犯這個相對大的代碼庫之前,我會很感激的一些想法或來自你們的反饋。
謝謝!
你只能通過你自定義的'make_shared'創建這些;如果您嘗試使用任何其他形式的初始化,它將在運行時失敗。只需使用提供創建實例的靜態'create()'函數的標準模式,然後在返回之前執行任何需要'shared_from_this()'的操作。 –
對不起,但我的團隊無法通過此類代碼審覈。你不能想出更簡單和自我記錄的設計嗎?你最終想要解決的問題是什麼?爲什麼傳統方法不能達到這個目標? –