reberhar@ethz-inf (Rolf Georg Eberhardt) (06/28/90)
hi everybody, I've encountered a problem concerning the [] operator. I've defined following class: typedef class foo *foo_p; class foo { public: foo_p next, list; // each object of foo may contain a list of other "foo's" foo_p operator [] (ndx); }; foo_p foo::operator [] (ndx) { foo_p tmp = list; // ... run thru list starting at list and return the ndx'th element return(tmp); }; I get following error msg's : void foo::do_something(foo_p b) { list[i]->next = b; // error : non pointer -> next b->next = a[i]; // error : bad assignment type: foo_p = foo ... }; In all C++ books I've looked into the []-operator must return a reference. Why - on the other hand - does following code compile and run correctly ? typedef class bb *bb_p; class bb { public: bb_p next; int key; // etc... }; class cc { public: bb_p list; bb_p operator[] (int ndx); // same code as above }; void main() { cc c; // do some initializing .. c[2]->key = 4; cout << c[2]->key; }; thanks in advance for your advice -- rolf eberhardt -- ETH Zuerich, Switzerland -- e-mail : reberhar@inf.ethz.ch
vaughan@puma.cad.mcc.com (Paul Vaughan) (06/29/90)
From: reberhar@ethz-inf (Rolf Georg Eberhardt) hi everybody, I've encountered a problem concerning the [] operator. I've defined following class: typedef class foo *foo_p; class foo { public: foo_p next, list; // each object of foo may contain a list of other "foo's" foo_p operator [] (ndx); }; foo_p foo::operator [] (ndx) { foo_p tmp = list; // ... run thru list starting at list and return the ndx'th element return(tmp); }; I get following error msg's : void foo::do_something(foo_p b) { list[i]->next = b; // error : non pointer -> next b->next = a[i]; // error : bad assignment type: foo_p = foo ... }; list is a foo*, list[i] is a foo--not the result of applying the operator [] to a foo, just the usual array [] on a foo*. Try (*list)[i] if you wanted to invoke foo::operator[]. In all C++ books I've looked into the []-operator must return a reference. Why - on the other hand - does following code compile and run correctly ? typedef class bb *bb_p; class bb { public: bb_p next; int key; // etc... }; class cc { public: bb_p list; bb_p operator[] (int ndx); // same code as above }; void main() { cc c; // do some initializing .. c[2]->key = 4; cout << c[2]->key; }; The operator[] can return whatever you want--no restriction to references. That's just a common usage. Here c is a cc (not a cc*, like above), thus c[2] invokes cc::operator[] which returns a bb*. The bb* (being a pointer) can be deferenced using -> to get the key field, so the code compiles fine. -- Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639 Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639 Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan
hannum@mahler.psu.edu (Charles Hannum) (07/06/90)
In article <27893@ethz-inf.UUCP> reberhar@ethz-inf (Rolf Georg Eberhardt) writes: ... In all C++ books I've looked into the []-operator must return a reference. ... You just answered your own question [B-)]. "Reference to T" and "pointer to T" are NOT the same. They are stored the same internally, but "reference to T" is treated exactly as if it were a "T", whereas "pointer to T" is distinct. For example, if one declares: struct foobar { int bar; } foo[20]; foo[] returns a foobar, not a foobar*. To demonstrate: foo[10]->bar = 0; // error: non-pointer -> bar (&foo[10])->bar = 0; // ok (In this case, we're forcing it to treat foo[] as a pointer by using the & operator.) This is what you really wanted to say before: foo[10].bar = 0; The reader can extrapolate this to the previous case. -- Virtually, Charles Martin Hannum "Those who say a thing cannot be done should Please send mail to: under no circumstances stand in the way of hannum@schubert.psu.edu he who is doing it." - a misquote
hannum@mahler.psu.edu (Charles Hannum) (07/06/90)
In article <1990Jul6.125741.28182@acc.stolaf.edu> hannum@mahler.psu.edu (Charles Hannum) writes:
...
foo[] returns a foobar, not a foobar*. To demonstrate:
...
Er, before someone else corrects me, let me do it myself:
foo[] returns a foobar&, not a foobar*. To demonstrate:
--
Virtually,
Charles Martin Hannum "Those who say a thing cannot be done should
Please send mail to: under no circumstances stand in the way of
hannum@schubert.psu.edu he who is doing it." - a misquote