X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fptr.h;h=89bb1d4ae9b62a25ed8e7ad649871991d2d26378;hp=93b16416f5471d9bd9eb165fd7d159767a3f2522;hb=18d403808b2c0fd73f43cbf4e346c5902f052a65;hpb=0a5049a0354466a88862c67b1b9c9e27e36cc309 diff --git a/ginac/ptr.h b/ginac/ptr.h index 93b16416..89bb1d4a 100644 --- a/ginac/ptr.h +++ b/ginac/ptr.h @@ -3,7 +3,7 @@ * Reference-counted pointer template. */ /* - * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2007 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 @@ -17,7 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef __GINAC_PTR_H__ @@ -80,11 +80,14 @@ public: 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; } @@ -139,6 +142,7 @@ public: inline friend std::ostream & operator<<(std::ostream & os, const ptr & rhs) { os << rhs.p; + return os; } private: