Article View: alt.comp.lang.learn.c-c++
Article #139Deleting copies of object destroys other copies' contents
From: Michael
Date: Fri, 26 Mar 1999 00:00
Date: Fri, 26 Mar 1999 00:00
35 lines
1970 bytes
1970 bytes
hey all, I have a question
If I have an object with a bunch of pointers to stuff, and the destructor of
that object goes and destroys all the pointers too, then what happens when I
have a copy of that object, where only the values of the pointers were
copied, and then the copy gets destroyed? How do I save the data the
pointer pointed to without making it invalid? (and hopefully without having
to write an operator= for it?
For instance
class Object
{
int *ptr;
~Object() {delete ptr;};
};
Object obj1;
obj1.ptr = new int; // say it now == 0x1234567
Object *obj2 = new Object;
*obj2 = obj1; // now obj2->ptr == obj1.ptr == 0x1234567 via
member-to-member (compiler-generated) copy
delete obj2; // this deletes ptr and makes *ptr bad data
obj1.ptr = ?????? // under MSVC this actually becomes == 0xafafafaf for easy
identification I guess.
Is there an easy way to save the value of ptr, or to copy the contents of
obj1.ptr to obj2->ptr without rewriting the operator= function?
thanks
ms
Technical Details
Message-ID:
<Fz_K2.8313$_47.7058@news2.giganews.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f79ee,bcdf27bb5f8413b,start X-Google-Attributes: gidf79ee,public X-Google-Thread: 109fba,bcdf27bb5f8413b,start X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,bcdf27bb5f8413b,start X-Google-Attributes: gid114592,public X-Google-Thread: 10d4ba,bcdf27bb5f8413b,start X-Google-Attributes: gid10d4ba,public From: "Michael" <&&schwager@u.arizona.edu> Subject: Deleting copies of object destroys other copies' contents Date: 1999/03/26 Message-ID: <Fz_K2.8313$_47.7058@news2.giganews.com> X-Deja-AN: 459615634 X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211 X-Trace: news2.giganews.com 922514789 208.164.98.153 (Sat, 27 Mar 1999 00:06:29 CDT) X-MSMail-Priority: Normal NNTP-Posting-Date: Sat, 27 Mar 1999 00:06:29 CDT Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++,it.comp.lang.c++