home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18226 < prev    next >
Encoding:
Text File  |  1992-12-21  |  3.8 KB  |  129 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cs.utexas.edu!torn!csd.unb.ca!news.ucs.mun.ca!morgan!kwong
  3. From: kwong@morgan.ucs.mun.ca (KAI S. WONG   ^.^)
  4. Subject: Make a deep copy!
  5. Message-ID: <kwong.724744711@morgan>
  6. Summary: Deep Copy
  7. Sender: usenet@news.ucs.mun.ca (NNTP server account)
  8. Organization: Memorial University of Newfoundland
  9. Date: Sat, 19 Dec 1992 05:58:31 GMT
  10. Lines: 117
  11.  
  12.  
  13.  
  14. In article <1992Dec11.232954.7071@borland.com>, pete@borland.com (Pete Becker) writes:
  15. |> In article <1992Dec11.170849.18525@rs6000.bham.ac.uk> pickerig@eee.bham.ac.uk (Guy Pickering) writes:
  16. |> >In ARM, the code for the `+` operator for the complex class is given as:
  17. |> >
  18. |> >inline complex operator+(complex a, complex b)
  19. |> >{
  20. |> >  return complex(a.re+b.re,a.im+b.im)
  21. |> >}
  22. |> >
  23. |> >In general, can I create an instance of a class in a member function and
  24. |> >return the object? Doesn't the complex object go out of scope?
  25. |> >
  26. |> 
  27. |>     Yes, it does.  But that's not the end of the inquiry.
  28. |> 
  29. |>     int add( int a, int b )
  30. |>     {
  31. |>     return a+b;
  32. |>     }
  33. |> 
  34. |>     Here, too, the temporary goes out of scope, but the compiler knows how
  35. |> to deal with that.  Same thing for an object of a user-defined type.
  36. |> 
  37.  
  38. How about if the structure is a bit more complex, especially one with
  39. dynamically allocated memory.  I tried the following (I appologize if it's
  40. syntactically incorrect, I no longer have the version which I actually tried.
  41. This one is just from memory):
  42.  
  43. class MATRIX
  44. {
  45. protected:
  46.     double      **entry;
  47.     int         nrows, ncols;
  48. public:
  49.     MATRIX( int num_rows, int num_cols );
  50.     MATRIX& operator=( const MATRIX& equivalent );
  51.     MATRIX& operator+( const MATRIX& addend );
  52. };
  53.  
  54. MATRIX::MATRIX(int num_rows, int num_cols )
  55. {
  56. register int    i;
  57.  
  58.     nrows = num_rows;
  59.     ncols = num_cols;
  60.  
  61.     entry = new double *[ nrows ];
  62.     for( i=0 ; i<nrows; i++ )
  63.         entry[i] = new double[ ncols ];
  64. }
  65.  
  66. MATRIX& MATRIX::operator=( const MATRIX& equivalent )
  67. {
  68. register int    i;
  69.  
  70.     for( i=0; i<nrows; i++)
  71.         memcpy( (void*)entry[i], 
  72.                 (void*)equivalent.entry[i],
  73.                 sizeof( double ) * ncols);
  74.  
  75.     return *this;
  76. }
  77.  
  78. MATRIX& MATRIX::operator+( const MATRIX& addend )
  79. {
  80. register int    i,j;
  81. MATRIX          sum(nrows, ncols);
  82.  
  83.     for( i=0 ; i<nrows ; i++ )
  84.         for ( j=0 ; j<ncols ; j++ )
  85.             sum.entry[i][j] = entry[i][j] + addend.entry[i][j];
  86.  
  87.     return sum;
  88. }
  89.  
  90.  
  91. Okay, the problem is when I tried to use the "+" operator as follows:
  92.  
  93.     matrix_c = matrix_a + matrix_b;
  94.  
  95. the "+" operation works and sum contains the proper matrix, that is until
  96. "+" finishes and goes out of scope.  The "return" statement makes a temporary
  97. copy of sum (ie. the values of its nrows, ncols, and the pointer entry), and
  98. then proceed to destroy sum automatically.  So, now the entry pointer of this
  99. temporary is no longer pointing to a valid block of memory!!   
  100.  
  101. Is this really what happened?  I am new to C++, I am not sure my interpretation
  102. is correct.  But all I can tell is that this simple addition and assignment 
  103. sequence DOESN'T work, and I don't know how to get around it.  HELP!!
  104.  
  105. gloria
  106.  
  107.  
  108. ----> Make A DEEP COPY will do. I think you can refer to your C++ books.
  109.  
  110.  
  111.  
  112.  
  113.  
  114. But they that wait upon the Lord shall renew their strength,
  115. they shall mount up with wings like eagles.
  116.                         Isaiah 40 : 31
  117.  
  118.  
  119. +-----------------------------------------------------------------------+
  120. |            . . .   . . . .               Faculty Of                   |
  121. |        +  +  +     +  +  +  +                Engineering              |
  122. |    *   *   *       *   *   *   *                 Electrical           |
  123. | K    a    i        W     O    N    G                                  |
  124. +-------------------------------------+--------+------------------------+
  125. | Memorial University Of Newfoundland | Canada | kwong@morgan.ucs.mun.ca|
  126. +-------------------------------------+--------+------------------------+
  127.  
  128.  
  129.