can implement this operator:
vec[0] = 5;
I mean, I know how to overload the bracket operator and also assignment operator, but how to overload both of them at the same time ? Is there any way ?
If you overload the [] operator it will work for both.
Define Copy assignment and operator[]()
Error : lvalue required as left operand of assignment while it works, if I write cout << vec[0];
Do you have an array pointer inside your vector class?
How to do this // declaration void operator [](); // implementation // ??? But I need to receive two arguments for this, first one as an index and the second one for the new element..
Elements are allocated in a pointer using malloc()
Use new instead. Share your class.
Look it up.
You can try to make the operator[] return a reference like this int& operator[](int indx){ return elem[indx]; }
elem is a pointer to an array in heap where all the elements are stored
Обсуждают сегодня