home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!usenet.ins.cwru.edu!agate!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!ulogic!hartman
- From: hartman@ulogic.UUCP (Richard M. Hartman)
- Newsgroups: comp.lang.c++
- Subject: Re: Make a deep copy!
- Message-ID: <769@ulogic.UUCP>
- Date: 22 Dec 92 22:41:34 GMT
- References: <kwong.724744711@morgan>
- Organization: negligable
- Lines: 49
-
- In article <kwong.724744711@morgan> kwong@morgan.ucs.mun.ca (KAI S. WONG ^.^) writes:
- :
- :
- :In article <1992Dec11.232954.7071@borland.com>, pete@borland.com (Pete Becker) writes:
- :>> In article <1992Dec11.170849.18525@rs6000.bham.ac.uk> pickerig@eee.bham.ac.uk (Guy Pickering) writes:
- :>> >In general, can I create an instance of a class in a member function and
- :>> >return the object? Doesn't the complex object go out of scope?
- :>>
- :>> Yes, it does. But that's not the end of the inquiry.
- :>>
- :>> int add( int a, int b )
- :>> {
- :>> return a+b;
- :>> }
- :>>
- :>> Here, too, the temporary goes out of scope, but the compiler knows how
- :>> to deal with that. Same thing for an object of a user-defined type.
- :>>
- :
- :How about if the structure is a bit more complex, especially one with
- :dynamically allocated memory. I tried the following (I appologize if it's
- :syntactically incorrect, I no longer have the version which I actually tried.
- :This one is just from memory):
- :
- [ ... example deleted ... ]
- :
- :the "+" operation works and sum contains the proper matrix, that is until
- :"+" finishes and goes out of scope. The "return" statement makes a temporary
- :copy of sum (ie. the values of its nrows, ncols, and the pointer entry), and
- :then proceed to destroy sum automatically. So, now the entry pointer of this
- :temporary is no longer pointing to a valid block of memory!!
- :
- :Is this really what happened? I am new to C++, I am not sure my interpretation
- :is correct. But all I can tell is that this simple addition and assignment
- :sequence DOESN'T work, and I don't know how to get around it. HELP!!
-
- I believe that these are the situations that indicate that you
- need to have a copy constructor and the operator=() defined for
- your class.
-
- I'm sure someone will correct me if I'm wrong. :)
-
-
- -Richard Hartman
- hartman@uLogic.COM
-
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- "Ideas are not responsible for the people who believe them."
-
-