Operator overloading problem

M

Matti

Hello,

I have a problem comparing 2 values of type AnsiString using the ==
operator (C++ Builder 5).

I use another class called TValue, which also defines the == operator.
Now when I
try to compare 2 AnsiString Values the == operator of class TValue is
invoked.

How can I avoid that and force my program to use the operator defined
for the
AnsiString class.

Here you can see a short example of what I mean.

TValue::TValue(AnsiString Val) {
}

bool TValue::eek:perator==(const TValue &iVal2) const {
return true;
}

test () {
AnsiString Val1, Val2;
Val1 = "Test";
Val2 = "Test";

if (Val1 == Val2) {
}
}

Thank you for your help,

Matti
 
G

Gianni Mariani

Matti said:
Hello,

I have a problem comparing 2 values of type AnsiString using the ==
operator (C++ Builder 5).

I use another class called TValue, which also defines the == operator.
Now when I
try to compare 2 AnsiString Values the == operator of class TValue is
invoked.

How can I avoid that and force my program to use the operator defined
for the
AnsiString class.

Here you can see a short example of what I mean.

TValue::TValue(AnsiString Val) {
}

bool TValue::eek:perator==(const TValue &iVal2) const {
return true;
}

test () {
AnsiString Val1, Val2;
Val1 = "Test";
Val2 = "Test";

if (Val1 == Val2) {
}
}

Thank you for your help,

A complete compilable example would really help.

It seems to me that if what your saying is true, your compiler is not
conforming to the standard. In the example, Val1 == Val2, you should
have the AnsiString operator == called. If what I read from the
standard is true, conversion + calling operator == should never be
preferred over just calling operator ==.
 
D

David B. Held

Matti said:
I have a problem comparing 2 values of type AnsiString
using the == operator (C++ Builder 5).
[...]

The best place to ask this is
borland.public.cppbuilder.language.cpp. But before you
ask your question there, reduce your problem to a self-
contained program of 50 lines or less, if you can. Odds
are, by the time you do that, you'll see that you have an
error someplace else.

Dave
 
S

Sergey Tolstov

Matti said:
Hello,

I have a problem comparing 2 values of type AnsiString using the ==
operator (C++ Builder 5).

I use another class called TValue, which also defines the == operator.
Now when I
try to compare 2 AnsiString Values the == operator of class TValue is
invoked.

How can I avoid that and force my program to use the operator defined
for the
AnsiString class.

Here you can see a short example of what I mean.

TValue::TValue(AnsiString Val) {
}

Use "explicit" keyword in your conversion constructor.

explicit TValue::TValue(AnsiString Val) {
}
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top