Operator overloading problem

Discussion in 'C++' started by Matti, Oct 11, 2003.

  1. Matti

    Matti Guest

    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
     
    Matti, Oct 11, 2003
    #1
    1. Advertising

  2. Matti wrote:
    > 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 ==.
     
    Gianni Mariani, Oct 11, 2003
    #2
    1. Advertising

  3. "Matti" <> wrote in message
    news:...
    >
    > 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



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
     
    David B. Held, Oct 11, 2003
    #3
  4. "Matti" <> wrote in message
    news:...
    > 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) {
    }
     
    Sergey Tolstov, Oct 11, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    434
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    751
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    742
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    571
  5. Replies:
    2
    Views:
    325
Loading...

Share This Page