c++ - A problem overloading [] in 2 variations -
this generic class:
template<class t, class prnt> class personalvec { public: personalvec(); t &operator[](int index) const; const t &operator[](int index) const; private: std::vector<t> _vec; };
i'm required implement 2 versions of [] operator:
1 return const reference , regular 1 return reference. when compile get:
personalvec.hpp:23: error: ‘const t& personalvec<t, prnt>::operator[](int) const’ cannot overloaded
personalvec.hpp:22: error: ‘t& personalvec<t, prnt>::operator[](int) const
i've put either 1 of them remark , compile, guess colliding somehow. problem , how can fix it?
thank you!
return type of function not criteria can used overloading of functions. function can overloaded if:
1. different no of arguments
2. differnt sequence of arguments or
3. different types of arguments
you trying overload function based on return type , hence gives error.
'const' keyword can overload functions if above 3 criterias not met. simple solution can make 1 of function const , keep other normal function
Comments
Post a Comment