* Reference-counted pointer template. */
/*
- * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
ptr &operator=(const ptr & other)
{
- // NB: must first add reference to "other", since other might be *this.
- other.p->add_reference();
+ // NB1: Must first add reference to "other", since other might be *this.
+ // NB2: Cache other.p, because if "other" is a subexpression of p,
+ // deleting p will also invalidate "other".
+ T *otherp = other.p;
+ otherp->add_reference();
if (p->remove_reference() == 0)
delete p;
- p = other.p;
+ p = otherp;
return *this;
}
inline friend std::ostream & operator<<(std::ostream & os, const ptr<T> & rhs)
{
os << rhs.p;
+ return os;
}
private: