Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: 'this' question Message-ID: <6590244@hplsla.HP.COM> Date: 11 Sep 89 17:45:35 GMT References: <14232@polyslo.CalPoly.EDU> Organization: HP Lake Stevens, WA Lines: 42 //> Wang: //> When I write: //> //> type_student* me = new type_student(); //> //> Is the address of 'me' guaranteed to be the same as the value of 'this' //> for the 'me' object? //Depends on what you consider the "address" of me, and what you consider the //"me" object. I presume by the "me object" you mean the structure referred to //by *me. Note that in C++ the object "me" is typically considered to be "me", //not "*me." This is different from other object oriented languages which //use pointers or handles for all objects. Similarly, I presume that by the //"address of me" you mean the address of the underlying object pointed to // by "me." In which case, consider: #include class bogus_student { const char* name; public: bogus_student(const char* Name) : name(Name) {} void print_name(){printf("I'm a student named %s, ",name);} void print_addresses() {printf("my this=%X but my address=%X\n", this, &(*this));} bogus_student* operator&(){return (bogus_student*)0x1234;} }; void main() { bogus_student& me = *(new bogus_student)("me"); me.print_name(); me.print_addresses(); bogus_student* me2 = new bogus_student("me2"); me2->print_name(); me2->print_addresses(); } //Hopefully, people would not overload the address-of operator "&" in ways //that will be incompatible with normal usage. Unfortunately, in reality //they will.