0

Due to project restrictions, I can't use boost or more modern C++. I have implemented a shared pointer that works for my project needs but I'm struggling with handling constant objects.

I need to be able to do something like -

SharedPtr<MyData> sp(new MyData()); SharedPtr<const MyData> const_sp(sp);

Andy
  • 3
  • 2
  • 2
    Show us what you've got so far. – LogicStuff Apr 27 '15 at 13:59
  • 2
    Check whether your environment has the TR1 extensions: http://stackoverflow.com/a/2918235/103167 Using `std::tr1::shared_ptr` will be a lot less painful than rolling your own, you've only scratched the surface of how complicated it gets. – Ben Voigt Apr 27 '15 at 14:10
  • I was looking for a quick and dirty shared pointer implementation. Due to nature of the project, didn't have access to either of those three libraries. Thanks for the link though! – Andy Apr 27 '15 at 19:56

1 Answers1

0

You could make a templated copy-constructor which will allow you to copy-construct from SharedPtr instances with a compatible template argument:

template <typename T>
class SharedPtr {
    template <typename U>
    SharedPtr (const SharedPtr<U> &rhs);
}
TartanLlama
  • 63,752
  • 13
  • 157
  • 193