Mail Archives: djgpp/1995/07/25/18:55:28
Ian Tester (94024831 AT athene DOT mit DOT csu DOT edu DOT au) wrote:
> Anyway, in a little test program, I declare a matrix with
> 'Matrix M;', and then latter initialize it with 'M = Matrix(3,3);'.
> Is this right? Should I declare it as a pointer ?
This could cause trouble, because you initialise M with a temporary
variable: "M" copies the pointer to the float-field, not the field
itself. As soon as the compilers deletes the temporary variable, the
destructor will probably delete the field, so the pointer within "M"
points to nowhere!
There are two solutions: implement a copy constructor which copies the
entire field instead of the pointer, or initialise the Matrix with:
Matrix M(3,3);
(you can declare your variables almost anywhere you like!)
> i.e 'Matrix *M;' and
> 'M = new Matrix(3,3);'
That yould be the third solution :)
Bye,
Jochen
--
------------------------------------
EMail: jscharrl AT ba-stuttgart DOT de
or: acorn1 AT ftp DOT uni-stuttgart DOT de
------------------------------------
- Raw text -