[comp.lang.c++] another probably dumb question

gamiddleton@orchid.waterloo.edu (Guy Middleton) (08/08/87)

I seem to have a problem with destructors.  In this code here:

	#include "string_class.h"

	main( int argc, char *argv[] ) {
		
		string x;

		if ((x = getstr()) != (string) NULL)
			print_string( x );
	}

... the constructor for the string class is called one more time than the
destructor.  If I change it to read:

		x = getstr();
		if (x != (string) NULL)
			print_string( x );

then they get called the same number of times.  I realise I probably
haven't provided enough information here, but I didn't want to post a huge
message.  Does anybody have any idea what might be going on?
__
 -Guy Middleton, University of Waterloo Institute for Computer Research
  gamiddleton@math.waterloo.edu, watmath!gamiddleton, gamiddleton@water.bitnet

alastair@geovision.UUCP (Alastair Mayer) (08/14/87)

In article <10148@orchid.waterloo.edu> gamiddleton@orchid.waterloo.edu (Guy Middleton) writes:
>
>I seem to have a problem with destructors.  In this code here:
>  [..small program..]
>... the constructor for the string class is called one more time than the
>destructor.  If I change it to read:
>  [..slightly reorganized program..]
>then they get called the same number of times.  I realise I probably
>haven't provided enough information here, but I didn't want to post a huge
>message.  Does anybody have any idea what might be going on?
>__
> -Guy Middleton, University of Waterloo Institute for Computer Research

I have some suspicions, but nothing concrete enough to put forward without
trying it myself.  I think I need more info. (Could it have something
to do with that cast of NULL to string?)
  However, as a general comment on trying to figure out just what is
going on when wierd things start happening with my C++ code, I often
find it helpful to look at the C code that cfront is generating ( -F
option).  Once you make your way past what it does to identifiers and
inline code, you can sometimes (not always!) figure out why it's doing
whatever it did.
-- 
 Alastair JW Mayer     BIX: al
                      UUCP: ...!utzoo!dciem!nrcaer!cognos!geovision!alastair

Let's build a base on the Moon on our way to the asteroids - forget Mars.