conversion operator problem

K

kowochen

i encounter a problem as following:

#include <iostream>
#include <string>
using namespace std;

class X
{
public:
X(const string& s, int i):s_(s),i_(i)
{}

operator string ()
{
return s_;
}

operator int ()
{
return i_;
}

private:
string s_;
int i_;
};

int main()
{
string s("abc");
int i(10);

X x("abc", 10);

if (x == i)
{
cout<<"int ok"<<endl;
}

return 0;
}


================================
x can be compared with the integer i and the output is "int ok",
but when i wanna compare with string s, it is a compile error:
if (x == s)
{
...
}

If convert x to string explicitly, it is ok:
if ((string)x == s)
{
...
}

Why couldn't it do a implicit type conversion to string like build in
type?
 
V

Victor Bazarov

i encounter a problem as following:

#include <iostream>
#include <string>
using namespace std;

class X
{
public:
X(const string& s, int i):s_(s),i_(i)
{}

operator string ()
{
return s_;
}

operator int ()
{
return i_;
}

private:
string s_;
int i_;
};

int main()
{
string s("abc");
int i(10);

X x("abc", 10);

if (x == i)
{
cout<<"int ok"<<endl;
}

return 0;
}


================================
x can be compared with the integer i and the output is "int ok",
but when i wanna compare with string s, it is a compile error:
if (x == s)
{
...
}

If convert x to string explicitly, it is ok:
if ((string)x == s)
{
...
}

Why couldn't it do a implicit type conversion to string like build in
type?

Probably because operator== for ints is _built_in_, and the
operator== for strings is a member function of 'std::string'.
Try

if (s == x)

and it should work.

V
 
A

Alf P. Steinbach

* (e-mail address removed):
i encounter a problem as following:

#include <iostream>
#include <string>
using namespace std;

class X
{
public:
X(const string& s, int i):s_(s),i_(i)
{}

operator string ()
{
return s_;
}

operator int ()
{
return i_;
}

private:
string s_;
int i_;
};

int main()
{
string s("abc");
int i(10);

X x("abc", 10);

if (x == i)
{
cout<<"int ok"<<endl;
}

return 0;
}


================================
x can be compared with the integer i and the output is "int ok",
but when i wanna compare with string s, it is a compile error:
if (x == s)
{
...
}

If convert x to string explicitly, it is ok:
if ((string)x == s)
{
...
}

Why couldn't it do a implicit type conversion to string like build in
type?

Comparision with built-in type int uses the built-in "==". This invokes
X::eek:perator int().

Comparision with std::string needs some custom "==". No such is found.
 
K

kowochen

Probably because operator== for ints is _built_in_, and the
operator== for strings is a member function of 'std::string'.
Try

if (s == x)

and it should work.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

i have tried this, it also doesn't wrok.
 
K

kowochen

* (e-mail address removed):


















Comparision with built-in type int uses the built-in "==". This invokes
X::eek:perator int().

Comparision with std::string needs some custom "==". No such is found.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -

Do u mean that implicit conversion only can be applied to built-in
"=="?
 
B

Bo Persson

(e-mail address removed) wrote:
::: * (e-mail address removed):
:::
:::
:::
:::
:::
:::: i encounter a problem as following:
:::
:::: #include <iostream>
:::: #include <string>
:::: using namespace std;
:::
:::: class X
:::: {
:::: public:
:::: X(const string& s, int i):s_(s),i_(i)
:::: {}
:::
:::: operator string ()
:::: {
:::: return s_;
:::: }
:::
:::: operator int ()
:::: {
:::: return i_;
:::: }
:::
:::: private:
:::: string s_;
:::: int i_;
:::: };
:::
:::: int main()
:::: {
:::: string s("abc");
:::: int i(10);
:::
:::: X x("abc", 10);
:::
:::: if (x == i)
:::: {
:::: cout<<"int ok"<<endl;
:::: }
:::
:::: return 0;
:::: }
:::
:::: ================================
:::: x can be compared with the integer i and the output is "int ok",
:::: but when i wanna compare with string s, it is a compile error:
:::: if (x == s)
:::: {
:::: ...
:::: }
:::
:::: If convert x to string explicitly, it is ok:
:::: if ((string)x == s)
:::: {
:::: ...
:::: }
:::
:::: Why couldn't it do a implicit type conversion to string like
:::: build in type?
:::
::: Comparision with built-in type int uses the built-in "==". This
::: invokes X::eek:perator int().
:::
::: Comparision with std::string needs some custom "==". No such is
::: found.
:::
::
:: Do u mean that implicit conversion only can be applied to built-in
:: "=="?

Not really.

One difference is that operator== for std::string (there are several)
are templates. None of them can be instantiated for type X, so they
are not considered.

To select an overload, the compiler first selects all possible
candidates, and then tries to choose the best match, only then
considering implicit conversions (and other things). In this case,
there are no candidates!


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top