Path: utzoo!attcan!uunet!mcsun!cernvax!chx400!ethz!ethz-inf!reberhar From: reberhar@ethz-inf (Rolf Georg Eberhardt) Newsgroups: comp.lang.c++ Subject: problems with [] operator Message-ID: <27893@ethz-inf.UUCP> Date: 28 Jun 90 15:07:57 GMT Sender: reberhar@inf.ethz.ch Organization: Informatik ETH Zurich Lines: 61 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