operator-

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

    #1

    operator-

    I have a class Foo
    class Foo {
    ...
    };
    for which I have overloaded the subtraction operator declared as
    int operator-(const Foo&, const Foo&);

    When I use this:
    Foo a, b;
    ...
    int n = a - b;
    the compiler looks for the implementation of
    int operator-(Foo, const Foo&);
    and does not use my operator. If I modify the code to be
    Foo a, b;
    ...
    int n = (const Foo&)(a) - (const Foo&)(b);
    everything is fine.

    I do not understand why I need to cast a and b.

  • Victor Bazarov

    #2
    Re: operator-

    wojtek wrote:[color=blue]
    > I have a class Foo
    > class Foo {
    > ...
    > };
    > for which I have overloaded the subtraction operator declared as
    > int operator-(const Foo&, const Foo&);
    >
    > When I use this:
    > Foo a, b;
    > ...
    > int n = a - b;
    > the compiler looks for the implementation of
    > int operator-(Foo, const Foo&);
    > and does not use my operator. If I modify the code to be
    > Foo a, b;
    > ...
    > int n = (const Foo&)(a) - (const Foo&)(b);
    > everything is fine.
    >
    > I do not understand why I need to cast a and b.
    >[/color]

    Do you also have operator int defined in your class, maybe?
    In any case, post complete minimal code that reproduces your
    problem, read FAQ 5.8.

    V

    Comment

    Working...