Wednesday, 15 April 2015

overloading - overload operator [] c++ -



overloading - overload operator [] c++ -

i want overload [], can do:

arrayclass x = arrayclass() someclass *sc = new someclass(); x[12] = sc;

there no problem simple types int, there problem see - happen 12th "cell" of arrayclass? how can phone call delete on it? e.g. like:

delete x[12]; x[12] = sc;

you need operator[] homecoming reference object. allow assigning.

someclass& operator[](size_t i);

unless array class holds pointers, there's no need phone call delete on element, ever. if holds pointers never exposes them caller, should handle necessary deletes itself.

c++ overloading operator-keyword

No comments:

Post a Comment