www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/07/25/18:55:28

Xref: news-dnh.mv.net comp.os.msdos.djgpp:1143
Path: news-dnh.mv.net!mv!news.sprintlink.net!tank.news.pipex.net!pipex!news.mathworks.com!fu-berlin.de!news.belwue.de!green.t-informatik.ba-stuttgart.de!jscharrl
From: jscharrl AT ba-stuttgart DOT de (Jochen Scharrlach)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: help with classes!
Date: 25 Jul 1995 14:02:58 GMT
Organization: Berufsakademie Stuttgart
Lines: 31
References: <Pine DOT SUN DOT 3 DOT 90 DOT 950725112635 DOT 19992B-100000 AT athene>
Reply-To: jscharrl AT ba-stuttgart DOT de
Nntp-Posting-Host: green.t-informatik.ba-stuttgart.de
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Dj-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019