overloading - C++ [] operator overload problem -


i'm still new c++ daily run new problems.

today came [] operator's turn:

i'm making myself new generic list class because don't std's one. i'm trying give warm , fuzzy of c#'s collections.generic list, want able access elements index. cut chase:

extract template:

t& operator[](int offset)     {         int translateval = offset - cursorpos;          movecursor(translateval);          return cursor->value;     }      const t& operator[](int offset) const     {         int translateval = offset - cursorpos;          movecursor(translateval);          return cursor->value;     } 

that's code operators. template uses "template", far saw on tutorials, that's correct way operator overloading.

nevertheless, when i'm trying access index, e.g.:

collections::list<int> *mylist; mylist = new collections::list<int>(); mylist->setcapacity(11); mylist->add(4); mylist->add(10); int = mylist[0]; 

i the

    no suitable conversion function "collections::list<int>" "int" exists 

error, referring "int = mylist[0]" line. "mylist[0]" type's still "collections::list", although should have been int. how come?

since mylist pointer mylist[0] doesn't invoke operator[], returns collections::list<int>*. want (*mylist)[0]. or better still, collections::list<int>& myref = *mylist; , use myref[0] (other option not allocate memory mylist on heap, can create on stack using collections::list<int> mylist , use . operator on it).


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -