A
Andrea Crotti
Looking on the internet looks like it's perfectly fine to overload
operators, but why then this doesn't work
--8<---------------cut here---------------start------------->8---
class Myclass
{
public:
int operator[](int index) { return 0; }
string operator[](int index) { string s = "ciao"; return s; }
};
--8<---------------cut here---------------end--------------->8---
and gives the error
--8<---------------cut here---------------start------------->8---
try.cpp:23: error: ‘std::string Myclass:perator[](int)’ cannot be overloaded
try.cpp:22: error: with ‘int Myclass:perator[](int)’
--8<---------------cut here---------------end--------------->8---
It would be very useful in another case (thread Globals), how otherwise
I can achieve the same result?
The only other way coming to my mind is another class with a template
parameter and then specializing it for the different types, but it
sounds a bit too much for such a simple thing...
operators, but why then this doesn't work
--8<---------------cut here---------------start------------->8---
class Myclass
{
public:
int operator[](int index) { return 0; }
string operator[](int index) { string s = "ciao"; return s; }
};
--8<---------------cut here---------------end--------------->8---
and gives the error
--8<---------------cut here---------------start------------->8---
try.cpp:23: error: ‘std::string Myclass:perator[](int)’ cannot be overloaded
try.cpp:22: error: with ‘int Myclass:perator[](int)’
--8<---------------cut here---------------end--------------->8---
It would be very useful in another case (thread Globals), how otherwise
I can achieve the same result?
The only other way coming to my mind is another class with a template
parameter and then specializing it for the different types, but it
sounds a bit too much for such a simple thing...