www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/09/22:46:20

From: "A. Sinan Unur" <sinan DOT unur AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Calling Class Constructors and Destructors
Date: Mon, 09 Feb 1998 19:10:21 -0500
Organization: Cornell University (http://www.cornell.edu/)
Lines: 29
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <34DF9AED.486D449C@cornell.edu>
References: <34df6b1d DOT 3108479 AT news DOT easynet DOT co DOT uk>
NNTP-Posting-Host: cu-dialup-0922.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Geoffrey French wrote:
> int main()
> {
>         TC x, *y;
> 
>         x = TC(1);
>         y = new TC(1);
> }
> 
> I find that upon calling the constructor TC(long i) in the line 'x =
> TC(1)' not only is the constructor called, but the destructor is
> called straight afterwards (!), thus deleteing the reserved memory for
> the variable a. However, in the line 'y = new TC(1)' only the
> constructor is called. It seems that whenever you say 'c =
> TClass(...)' the TClass destructor is called as well as the
> constructor.

by the time

  x = TC(1);

has been executed, x has already been initialized using the default
constructor. you can use:

  TC x(1);

to use the alternate constructor.

  -- Sinan

- Raw text -


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