|
|
От: |
okman
|
https://searchinform.ru/ |
| Дата: | 24.03.18 04:15 | ||
| Оценка: | |||
To explicitly or implicitly convert a pointer (a glvalue) referring to an object of class X to a
pointer (reference) to a direct or indirect base class B of X, the construction of X and the construction
of all of its direct or indirect bases that directly or indirectly derive from B shall have
started and the destruction of these classes shall not have completed, otherwise the conversion
results in undefined behavior.
//
// The "obvious" converting constructor implementation:
//
// template<class Y>
// weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn)
// {
// }
//
// has a serious problem.
//
// r.px may already have been invalidated. The px(r.px)
// conversion may require access to *r.px (virtual inheritance).
//
// It is not possible to avoid spurious access violations since
// in multithreaded programs r.px may be invalidated at any point.
//shared_ptr<X1> ptr = ...;
weak_ptr<X1> w1(ptr);
ptr.reset();
weak_ptr<Base> w2(w1); // BANG!