std :: shared_ptr을 다운 캐스트하는 방법은 무엇입니까?
중히 여기다:
struct SomethingThatsABase
{
virtual bool IsChildOne() const { return false; }
virtual bool IsChildTwo() const { return false; }
};
struct ChildOne : public SomethingThatsABase
{
virtual bool IsChildOne() const { return true; }
};
struct ChildTwo : public SomethingThatsABase
{
virtual bool IsChildTwo() const { return true; }
};
void SomeClientExpectingAChildOne(std::shared_ptr<ChildOne> const& ptrOne)
{
//Does stuff
}
void SomeClient(std::shared_ptr<SomethingThatsABase> const& ptr)
{
if (ptr->IsChildOne())
{
SomeClientExpectingAChildOne(ptr); //Oops.
//Hmm.. can't static_cast here, because we need a `shared_ptr` out of it.
}
}
( std::shared_ptr<ChildOne>(static_cast<ChildOne*>(ptr.get()))
참조 카운트가 두 shared_ptr
s 간에 공유되지 않기 때문에 간단히 할 수는 없습니다. )
이것은 작동해야합니다.
if (ptr->IsChildOne())
{
SomeClientExpectingAChildOne(std::static_pointer_cast<ChildOne>(ptr));
}
에 shared_ptr
해당하는 static_cast
것은 static_pointer_cast
이고에 shared_ptr
해당하는 dynamic_cast
것은 dynamic_pointer_cast
입니다.
C ++ 11부터 C ++ 표준의 §20.10.2.2.9 ( [util.smartptr.shared.cast] )는 static_cast
, const_cast
및 dynamic_cast
for std::shared_ptr
에 해당하는 항목을 다음과 같이 지정합니다.
std::static_pointer_cast
:
template <class T, class U>
shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r) noexcept;
static_pointer_cast
요구가 static_cast<T *>(r.get())
잘 형성된다. 경우 r
빈, 비어 shared_ptr<T>
그렇지 않으면 포인터 반환, 반환 w
과 함께 공유 소유권 r
곳 w.get() == static_cast<T *>(r.get())
과 w.use_count() == r.use_count()
.
std::const_pointer_cast
:
template <class T, class U>
shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r) noexcept;
const_pointer_cast
has similar requirements and semantics to static_pointer_cast
, except that const_cast
is used instead of static_cast
.
std::dynamic_pointer_cast
:
template <class T, class U>
shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r) noexcept;
dynamic_pointer_cast
is a bit different as it requires dynamic_cast<T *>(r.get())
to be well formed and have well defined semantics. If dynamic_cast<T *>(r.get())
is a non-zero value, returns a pointer w
sharing ownership with r
where w.get() == dynamic_cast<T *>(r.get())
and w.use_count() == r.use_count()
, otherwise an empty shared_ptr<T>
is returned.
std::reinterpret_pointer_cast
:
For C++17, N3920 (adopted into Library Fundamentals TS in February 2014) also proposed a std::reinterpret_pointer_cast
similar to the above, which would only require reinterpret_cast<T *>((U *) 0)
to be well formed and returns shared_ptr<T>(r, reinterpret_cast<typename shared_ptr<T>::element_type *>(r.get()))
. Note N3920 also changed the wording for the other shared_ptr
casts and extended shared_ptr
to support arrays.
참고URL : https://stackoverflow.com/questions/6795629/how-does-one-downcast-a-stdshared-ptr
'code' 카테고리의 다른 글
SQL Server 2005의 VARBINARY 필드 크기 (0) | 2020.12.07 |
---|---|
전체를 채우기 위해 태그 (0) | 2020.12.07 |
Backbone의 모델을 초기 기본값으로 재설정하는 가장 쉬운 방법은 무엇입니까? (0) | 2020.12.07 |
android-프로그래밍 방식으로 LayoutParams 설정 (0) | 2020.12.07 |
Ace Cloud 9 편집기에서 콘텐츠 높이 자동 조정 (0) | 2020.12.07 |