Path: utzoo!attcan!uunet!world!decwrl!wuarchive!emory!gatech!uflorida!travis!tom From: tom@ssd.csd.harris.com (Tom Horsley) Newsgroups: comp.lang.c++ Subject: Constructor called with reference to self? Message-ID: Date: 2 Nov 90 12:22:13 GMT Sender: news@travis.csd.harris.com Organization: Harris Computer Systems Division Lines: 46 Given a class that looks like: class someclass { public: someclass(someclass& arg); ... } 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? I hope the answer is 'NO' since all the C++ books emphasize that constructors only get raw bits and it is their job to initialize those bits. So if '*this' is raw bits and 'arg' is a reference to an already existing class, how can they be located at the same point in memory? 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++). Does this look as much like a bug to everyone else as it does to me, or have I possibly encountered one of those subtle gotcha's that always seem to attract programmers who are just starting to use a new language? -- ====================================================================== domain: tahorsley@csd.harris.com USMail: Tom Horsley uucp: ...!uunet!hcx1!tahorsley 511 Kingbird Circle Delray Beach, FL 33444 +==== Censorship is the only form of Obscenity ======================+ | (Wait, I forgot government tobacco subsidies...) | +====================================================================+