Smart pointer passing

I have a smart pointer - code below, when I pass it into this function:

virtual void Add(const IPointer<Element> ItemElement, TuInt Index = 0)

it does not get copied, and thus decrements the reference count when the function exits, without having incremented the reference count when it enters the function. Why is this?

template <class Element> class IPointer
{
private:
  Element *m_pElement ;
protected:
  virtual TuInt RemoveReference() const
  {
    ...
  }

  virtual void AddReference(Element
  *Reference) const
  {
    ...
  }
public:
  IPointer() {
    m_pElement = 0 ;
  }

  IPointer(Element *Pointer) {
    m_pElement = 0 ;
    AddReference(Pointer) ;
  }

  template<class newType> IPointer
  (const IPointer<newType> &Pointer) {
    m_pElement = 0 ;
    Element *l_pElement =
      dynamic_cast<Element *>
      (Pointer.operator->()) ;
    AddReference(l_pElement) ;
  }

  virtual ~IPointer() {
    RemoveReference() ;
  }

  template<class newType>
  IPointer<Element> &operator=(const
  IPointer<newType> &Pointer) {
    Element *l_pElement =
      dynamic_cast<Element *>
      (Pointer.operator->()) ;
    AddReference(l_pElement) ;
    return *this ;
  }

  Element *operator->() const {
    return m_pElement ;
  }

  Element &operator*() const {
    return *m_pElement ;
  }

  const TBoolean get_IsNull() const {
    return(m_pElement == 0) ;
  }

  operator IPointer<const Element>()
  const {
    return IPointer<const Element>
      (m_pElement) ;
  }

  template<class newType> operator
  IPointer<newType>()
  {
    newType *l_pNew =
      dynamic_cast<newType *>
      (m_pElement) ;
    return IPointer<newType>(l_pNew) ;
  }
};
SeanHAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gvgCommented:
When you call Add a copy of the smart pointer is made so you must have a copy constructor.

IPointer( const IPointer& pointer )

When you don't supply copy constructor C++ does it for you as a bitwise copy and that is no good for Smart Pointers.

Gunnar Valur
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SeanHAuthor Commented:
Your answer works, and I've awarded you the points, but can you please tell me why this copy constructor doesn't work:

  template<class newType> IPointer
  (const IPointer<newType> &Pointer) {
    m_pElement = 0 ;
    Element *l_pElement =
      dynamic_cast<Element *>
      (Pointer.operator->()) ;
    AddReference(l_pElement) ;
  }

Surely in the case where it is copying a pointer of its type, it resolves to

IPointer<Type>(const IPointer<Type>& Pointer)

Thanks
0
gvgCommented:
Sorry I do not know why it doesn't work.  Might have something to do with that copy constructors are special functions.  ( At least so special that C++ impliments them for you if you don't do it you self )  If you find the explaination please tell me.

Gunnar Valur.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.