Paul
2009-10-29 12:11:01 UTC
struct Cref {
char c;
operator const char&() const { return c; }
};
struct Sref {
std::string s;
operator const std::string&() const { return s; }
};
int main()
{
Cref cr;
if (cr == 'a') return 1;
Sref sr;
if (sr == "a") return 2; // same if "a" is replced with std::string("a") or
const std::string&
}
The above code works for cr but not for sr and I wonder why and how to make
it work. I am trying to use a reference type in a way similar to how
Stroustrup does this for strings in his example (p. 296) and just stumbled
upon this.
Thank you.
Paul
char c;
operator const char&() const { return c; }
};
struct Sref {
std::string s;
operator const std::string&() const { return s; }
};
int main()
{
Cref cr;
if (cr == 'a') return 1;
Sref sr;
if (sr == "a") return 2; // same if "a" is replced with std::string("a") or
const std::string&
}
The above code works for cr but not for sr and I wonder why and how to make
it work. I am trying to use a reference type in a way similar to how
Stroustrup does this for strings in his example (p. 296) and just stumbled
upon this.
Thank you.
Paul