Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!sdd.hp.com!ucsd!ucbvax!galileo.berkeley.edu!jbuck From: jbuck@galileo.berkeley.edu (Joe Buck) Newsgroups: comp.lang.c++ Subject: Re: Constructor called with reference to self? Message-ID: <39359@ucbvax.BERKELEY.EDU> Date: 3 Nov 90 00:34:30 GMT References: Sender: usenet@ucbvax.BERKELEY.EDU Reply-To: jbuck@galileo.berkeley.edu (Joe Buck) Lines: 35 In article , tom@ssd.csd.harris.com (Tom Horsley) writes: |> Is it ever possible with a correctly functioning C++ compiler for your |> program to reach the body of the if statement in the following copy |> constructor: |> |> someclass::someclass(someclass& arg) |> { |> if (this == &arg) { |> // Can I get here? |> } |> } |> |> In other words can a copy constructor ever be called with a reference to |> self as its argument? What's probably happened is that your program has a memory allocation bug. Specifically, you've deleted an object but you still have a pointer to it lying around, so you're trying to use an object that you have deleted. Neophytes always think they've found a compiler bug when this bites them. |> I spent several hours last night determining that this is actually happening |> in a Turbo C++ program I wrote in which I have overloaded a lot of |> arithmetic operators and have written some complex expressions involving |> these overloaded operators. When I modified my constructors to detect this |> case and avoid clobbering the information it is about to copy, the program |> started working flawlessly (it worked on two different g++ implementations |> before I tried it on Turbo C++). You either have a bug in your memory allocation somewhere that didn't bite you under g++ for some reason, or there's a bug in one of the Turbo C++ classes that you are using. -- Joe Buck jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck