I hope someone might be able to answer why the following doesn\'t work. Bear with me though, I am still very much a noob... I just cannot get to the bottom of why the follow
Alright, thanks a lot already everyone. I think I am starting to get the hang of it, kind of...
First of all I wasn't aware of the fact, that char is just an 8-bit int. Thanks for that clarification.
So I understand that, because there are three assignment operators defined for std::string, each with different argument (string, char*, const char*) the right-hand-side of my expression
s=t
doesn't know, which type is has to convert into, since there are multiple, potentially matching (for this assignment to std::string) conversions defined with either
operator int () {return 77;};
operator std::string () {return "hello";};
(since char : 8bit int)
or
operator char* () {return (char*)"hi";};
operator std::string () {return "hello";};
Is that right? So in idiots terms, the left-hand-side of the assignment isn't telling the right-hand-side which type it expects, so rhs has to choose from its options, where one is as good as some other? std::string operator= is being to tolerant for my intents?
So far so good, I thought I got it - but then, why does the following create ambiguity as well?
using namespace std;
#include
#include
class testClass
{
public:
operator float () {return float(77.333);};
operator std::string () {return "hello";};
};
int main()
{
std::string s = "goodday";
testClass t;
s = t;
cout<< " string: "<
Now there is only one matching conversion operator defined by me, right? std::string operator= cannot take floats, or can it? Or is float in some way equivalent to some variant of char again?
I understand the code as 's=' telling the rhs: "give me a string, char* or const char*"
Rhs checks what it can provide given an instance of testClass, and the only match is testClass::operator std::string
Again, thanks for your patience, expertise and time guys - I really appreciate it.