Path: utzoo!attcan!uunet!samsung!usc!apple!snorkelwacker!spdcc!merk!alliant!linus!luke.mitre.org!dsr From: dsr@luke.mitre.org (Douglas S. Rand) Newsgroups: comp.lang.c++ Subject: Re: testing this==0 Keywords: this Message-ID: <111892@linus.mitre.org> Date: 27 Jun 90 13:30:22 GMT References: <1990Jun26.175953.16763@uncecs.edu> Sender: usenet@linus.mitre.org Reply-To: dsr@luke.mitre.org (Douglas S. Rand) Distribution: comp Lines: 41 In article <1990Jun26.175953.16763@uncecs.edu>, harris@uncecs.edu (Mark Harris) writes: |> |>Help! I'm fairly new to C++, and I'm running into unexpected |>behavior from my new Borland compiler. I thought that the value |>of 'this' was supposed to be zero at the start of an object's |>constructor if the object is being created on the heap. I |>expected output |> nonzero |> zero |>in the program below, but I get |> nonzero |> nonzero |>What gives? |> |>#include |>struct test{ |> int a; |> test(){ |> if(this==0)puts("zero"); |> else puts("nonzero"); |> } |>}; |> |>void main() |>{ |> test t; |> test *x=new test; |>} |> `this' can never be zero in a constructor. It's just been malloc'd or allocated on the heap and already has a distinct address. If this wasn't true you couldn't assigned values to the instance variables, you'd have a zero pointer dereference. Douglas S. Rand Internet: Snail: MITRE, Burlington Road, Bedford, MA Disclaimer: MITRE might agree with me - then again...