假设我有一个 typeAggregator
和一个Aggregatee
。前者知道 s 对后者的集合shared_ptr
。后者有一个指向前者的唯一反向指针:
struct Aggregatee {
private:
Aggregator& aggregator;
Aggregatee(Aggregator& aggregator)
: aggregator{aggregator} {
// PROBLEM HERE:
// I want to put `this` into the aggregation list,
// but I have no clue how to properly refer to `this`
// because I don't have the smart pointer object, yet.
}
Aggregatee(Aggregatee const& from) { /* ... */ }
public:
template <typename Args...>
static std::shared_ptr<Aggregatee> create(Args... args) {
return std::make_shared<Aggregatee>(args...);
}
};
struct Aggregator {
std::set<std::shared_ptr<Aggregatee>> aggregation;
};
显然,我可以在使用私有函数之后推迟Aggregatee
在Aggregator
对象中注册,但它闻起来像两阶段,因为对象临时不一致地初始化。make_shared
register
有什么已知的解决方案吗?