I'm still new to C ++, so I run into new issues every day.
Today came the rotation of the operator []:
I am creating a new generic List class because I don't really like std. I'm trying to give it a warm and fuzzy look at the C # Collections.Generic List, so I want to have access to items by index. To interrupt the chase:
Excerpt from the 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; }
This is the code for operators. A template uses a "template", so as far as I've seen in some tutorials, this is the correct way to overload an operator.
However, when I try to access by index, for example:
Collections::List<int> *myList; myList = new Collections::List<int>(); myList->SetCapacity(11); myList->Add(4); myList->Add(10); int a = myList[0];
I get
no suitable conversion function from "Collections::List<int>" to "int" exists
referring to the string "int a = myList [0]". Basically the "myList [0]" type is still "Collections :: List", although it should have been just an int. How to come?
source share