Operator overloading problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matti

    Operator overloading problem

    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::operato r==(const TValue &iVal2) const {
    return true;
    }

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

    if (Val1 == Val2) {
    }
    }

    Thank you for your help,

    Matti
  • Gianni Mariani

    #2
    Re: Operator overloading problem

    Matti wrote:[color=blue]
    > 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::operato r==(const TValue &iVal2) const {
    > return true;
    > }
    >
    > test () {
    > AnsiString Val1, Val2;
    > Val1 = "Test";
    > Val2 = "Test";
    >
    > if (Val1 == Val2) {
    > }
    > }
    >
    > Thank you for your help,[/color]

    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 ==.



    Comment

    • David B. Held

      #3
      Re: Operator overloading problem

      "Matti" <xmrf@yahoo.d e> wrote in message
      news:a4b6eb66.0 310111046.2be4e 9d0@posting.goo gle.com...[color=blue]
      >
      > I have a problem comparing 2 values of type AnsiString
      > using the == operator (C++ Builder 5).
      > [...][/color]

      The best place to ask this is
      borland.public. cppbuilder.lang uage.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


      Comment

      • Sergey Tolstov

        #4
        Re: Operator overloading problem


        "Matti" <xmrf@yahoo.d e> wrote in message
        news:a4b6eb66.0 310111046.2be4e 9d0@posting.goo gle.com...[color=blue]
        > 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) {
        > }[/color]

        Use "explicit" keyword in your conversion constructor.

        explicit TValue::TValue( AnsiString Val) {
        }



        Comment

        Working...