home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!cs.utexas.edu!torn!csd.unb.ca!news.ucs.mun.ca!morgan!kwong
- From: kwong@morgan.ucs.mun.ca (KAI S. WONG ^.^)
- Subject: Make a deep copy!
- Message-ID: <kwong.724744711@morgan>
- Summary: Deep Copy
- Sender: usenet@news.ucs.mun.ca (NNTP server account)
- Organization: Memorial University of Newfoundland
- Date: Sat, 19 Dec 1992 05:58:31 GMT
- Lines: 117
-
-
-
- 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 ARM, the code for the `+` operator for the complex class is given as:
- |> >
- |> >inline complex operator+(complex a, complex b)
- |> >{
- |> > return complex(a.re+b.re,a.im+b.im)
- |> >}
- |> >
- |> >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):
-
- class MATRIX
- {
- protected:
- double **entry;
- int nrows, ncols;
- public:
- MATRIX( int num_rows, int num_cols );
- MATRIX& operator=( const MATRIX& equivalent );
- MATRIX& operator+( const MATRIX& addend );
- };
-
- MATRIX::MATRIX(int num_rows, int num_cols )
- {
- register int i;
-
- nrows = num_rows;
- ncols = num_cols;
-
- entry = new double *[ nrows ];
- for( i=0 ; i<nrows; i++ )
- entry[i] = new double[ ncols ];
- }
-
- MATRIX& MATRIX::operator=( const MATRIX& equivalent )
- {
- register int i;
-
- for( i=0; i<nrows; i++)
- memcpy( (void*)entry[i],
- (void*)equivalent.entry[i],
- sizeof( double ) * ncols);
-
- return *this;
- }
-
- MATRIX& MATRIX::operator+( const MATRIX& addend )
- {
- register int i,j;
- MATRIX sum(nrows, ncols);
-
- for( i=0 ; i<nrows ; i++ )
- for ( j=0 ; j<ncols ; j++ )
- sum.entry[i][j] = entry[i][j] + addend.entry[i][j];
-
- return sum;
- }
-
-
- Okay, the problem is when I tried to use the "+" operator as follows:
-
- matrix_c = matrix_a + matrix_b;
-
- 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!!
-
- gloria
-
-
- ----> Make A DEEP COPY will do. I think you can refer to your C++ books.
-
-
-
-
-
- But they that wait upon the Lord shall renew their strength,
- they shall mount up with wings like eagles.
- Isaiah 40 : 31
-
-
- +-----------------------------------------------------------------------+
- | . . . . . . . Faculty Of |
- | + + + + + + + Engineering |
- | * * * * * * * Electrical |
- | K a i W O N G |
- +-------------------------------------+--------+------------------------+
- | Memorial University Of Newfoundland | Canada | kwong@morgan.ucs.mun.ca|
- +-------------------------------------+--------+------------------------+
-
-
-