ttwang@polyslo.CalPoly.EDU (Thomas Wang) (09/07/89)
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? -Thomas Wang ("Ole wa onna da!!!" - from Urusei Yatsura) ttwang@polyslo.calpoly.edu
jima@hplsla.HP.COM (Jim Adcock) (09/12/89)
//> 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 <stdio.h> 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.
zweig@brutus.cs.uiuc.edu (Johnny Zweig) (09/12/89)
>//> 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? Guaranteed not to be. &me would be the address of the pointer me. The VALUE stored in me has to be the same as the address of the type_student object, for example: type_student::f( type_student * p ) { printf("%x %x\n", this, p ); } will print the same number twice if you were to call me->f( me );. "this" is defined as a pointer to the object -- I imagine it would break a lot of code if somehow a pointer to object X and X.this couldn't be compared with == sensibly. -Johnny this