ProblemPtrProblem::create(FrameStructure_frame_structure){ProblemPtrp(newProblem(_frame_structure));// We use `new` and not `make_shared` since the Problem constructor is private and cannot be passes to `make_shared`.p->setup();returnp->shared_from_this();}
Explanation:
Problem (in short, P) has three shared_ptr to H, T and M, and each of these has a weak_ptr to P.
For the sake of a good user API, I want all these pointers to be valid just after creating P.
At construction time, shared_from_this() does not work
Therefore, I cannot link H, T, M to P when I am constructing P:
H::problem_ptr = shared_from_this(); does not work!
Solution: make constructor P() private, and write a factory method P::create() as in the code above
New Issue: To construct a ProblemPtr (aka shared_ptr<P>), I cannot use make_shared<P>, because it needs to call P() , and it is private:
ProblemPtr p = make_shared<P>(_frame_structure) does not work!
Solution: I call the shared_ptr constructor with the new syntax and not the make_shared:
Is your issue similar to this? In `problem.cpp`:
```c++
ProblemPtr Problem::create(FrameStructure _frame_structure)
{
ProblemPtr p(new Problem(_frame_structure)); // We use `new` and not `make_shared` since the Problem constructor is private and cannot be passes to `make_shared`.
p->setup();
return p->shared_from_this();
}
```
Explanation:
- Problem (in short, `P`) has three `shared_ptr` to `H`, `T` and `M`, and each of these has a `weak_ptr` to `P`.
- For the sake of a good user API, I want all these pointers to be valid just after creating `P`.
- At construction time, `shared_from_this()` does not work
- Therefore, I cannot link `H`, `T`, `M` to `P` when I am constructing `P`:
- `H::problem_ptr = shared_from_this();` does not work!
- Solution: make constructor `P()` private, and write a factory method `P::create()` as in the code above
- New Issue: To construct a `ProblemPtr` (aka `shared_ptr<P>`), I cannot use `make_shared<P>`, because it needs to call `P()` , and it is private:
- `ProblemPtr p = make_shared<P>(_frame_structure)` does not work!
- Solution: I call the `shared_ptr` constructor with the `new` syntax and not the `make_shared`:
- `ProblemPtr p(new Problem(_frame_structure));` works!