I'm to tired to figure out how to get the base and derived class configured to work properly much less the pointer casts
This commit is contained in:
parent
d90f19f897
commit
12d0c45322
1 changed files with 5 additions and 2 deletions
|
@ -16,7 +16,7 @@ template <typename T> struct Base {
|
|||
|
||||
T *ptr;
|
||||
Base(T *ptr) : ptr{ptr} {}
|
||||
template <typename U> Base(Base<U> const &ptr) : ptr{ptr.ptr} {}
|
||||
template <typename U> Base(Base<U> &ptr) : ptr{ptr.ptr} {}
|
||||
|
||||
public:
|
||||
virtual T &operator*() { return *ptr; }
|
||||
|
@ -28,6 +28,7 @@ template <typename T, typename U> class MyDerived : public Base<T> {
|
|||
|
||||
public:
|
||||
MyDerived(U *ptr) : Base<T>{ptr} { this->ptr = ptr; }
|
||||
MyDerived(Base<U> &ptr) : Base<T>{ptr.ptr} { this->ptr = ptr.ptr; }
|
||||
U &operator*() { return *ptr; }
|
||||
U *operator->() { return ptr; }
|
||||
virtual ~MyDerived() { delete this->ptr; }
|
||||
|
@ -46,7 +47,9 @@ public:
|
|||
static_cast<std::size_t>(ptr ? 1 : 0)}} {}
|
||||
SharedPtr(const SharedPtr &p) : ptr{p.ptr}, count{p.count} {}
|
||||
template <typename U>
|
||||
SharedPtr(const SharedPtr<U> &p) : ptr(p.ptr), count{p.count} {
|
||||
SharedPtr(const SharedPtr<U> &p)
|
||||
: ptr(MyDerived<U, T>(const_cast<cs440::Base<U> &>(*p.ptr))),
|
||||
count{p.count} {
|
||||
(*count)++;
|
||||
}
|
||||
SharedPtr(SharedPtr &&p) : ptr{p.ptr} { p.ptr = nullptr; }
|
||||
|
|
Loading…
Reference in a new issue