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;
|
T *ptr;
|
||||||
Base(T *ptr) : ptr{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:
|
public:
|
||||||
virtual T &operator*() { return *ptr; }
|
virtual T &operator*() { return *ptr; }
|
||||||
|
@ -28,6 +28,7 @@ template <typename T, typename U> class MyDerived : public Base<T> {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MyDerived(U *ptr) : Base<T>{ptr} { this->ptr = ptr; }
|
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; }
|
||||||
U *operator->() { return ptr; }
|
U *operator->() { return ptr; }
|
||||||
virtual ~MyDerived() { delete this->ptr; }
|
virtual ~MyDerived() { delete this->ptr; }
|
||||||
|
@ -46,7 +47,9 @@ public:
|
||||||
static_cast<std::size_t>(ptr ? 1 : 0)}} {}
|
static_cast<std::size_t>(ptr ? 1 : 0)}} {}
|
||||||
SharedPtr(const SharedPtr &p) : ptr{p.ptr}, count{p.count} {}
|
SharedPtr(const SharedPtr &p) : ptr{p.ptr}, count{p.count} {}
|
||||||
template <typename U>
|
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)++;
|
(*count)++;
|
||||||
}
|
}
|
||||||
SharedPtr(SharedPtr &&p) : ptr{p.ptr} { p.ptr = nullptr; }
|
SharedPtr(SharedPtr &&p) : ptr{p.ptr} { p.ptr = nullptr; }
|
||||||
|
|
Loading…
Reference in a new issue