perm@mizar.docs.uu.se (Per Mildner) (06/01/89)
Jonathan Shopiro and Jerry Schwarz of AT&T both supplied the correct code to do what I wan't. operator-> should be declared to return a pointer. I still don't understand why g++ refuses to inline code it. class Object { public: int a; }; class Pointer { int data; public: Object* p; Object* operator ->(); Object& operator *(); }; inline Object* Pointer::operator ->() { // won't inline return p; }; inline Object& Pointer::operator *() { // inline ok return *p; }; int main (int argc, char* argv[]) { Object O; Pointer P; P.p = &O; (*P).a = 20; // why does this inline P->a = 10; // and not this? return P->a; }; /* [g++-1.35.1-] mizar 3% g++ -Wall -O -g operator-test.cc operator-test.cc:18: warning: inline declaration ignored for function with `...' */
cs88rfj@unccvax.uncc.edu (Roger Johnson) (11/14/90)
I am new to c++, and am writing a graphical interface program which maintains a linked list of shapes. In the code fragement below, I am type casting the list pointers (normally (void *)) to (Shape *). I've looked at overloading the '->' operator but the documentation I have is not very good. I want to overload '->' in the class "Shape", so the '->' will be a pointer to t object Shape. Any suggestions? Class Declarations: class List { public: void Add_cell (void *p, void *n) { prev = p; next = n; }; virtual char *is_A (void) {}; protected: void *Next (void) { return (void *) this->next; }; void Next (void *n) { this->next = n; }; void Previous (void *p) { this->prev = p; }; void *Previous (void) { return (void *) this->prev; }; private: void *prev, *next; }; class Shape : protected List { public: virtual int contains (int x, int y) {}; void check (int x, int y) { Shape *temp; temp = this; while (temp) { if (temp->contains(x,y)) { if (temp->state) printf ("%s: %c selected\n",temp->is_A(),temp->id); else printf ("%s: %c deselected\n",temp->is_A(),temp->id); temp = NULL; } else temp = (Shape *)temp->Previous(); }; }; Shape * del (void) { int deleted = 0; Shape *delptr, *temp = this; while (temp) { if (temp->state) { if (temp->Previous()) ((Shape *)temp->Previous())->Next(temp->Next()); if (temp->Next()) ((Shape *)temp->Next())->Previous(temp->Previous()); else this = (Shape *)this->Previous(); delptr = temp; temp = (Shape *)temp->Previous(); delete delptr; deleted = 1; } else temp = (Shape *)temp->Previous(); } if (deleted) { printf ("The selected shape(s) have been deleted\n"); deleted = 0; } return this; }; protected: int level, state; char id; void add (char n) { if (Previous()) level = ((Shape *) Previous())->level+1; else level = 0; state = 0; id = n; printf ("The %s: %c has been added\n",this->is_A(),id); }; int toggle (void) { if (state) state = 0; else state = 1; return state; }; }; Any replys can also be E-mailed to cs88rfj@unccvax.uncc.edu Thanks in advance. --Roger Johnson, :-( "Wish I could figure this one out!"