By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

operator-

P: n/a
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.

Dec 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
wojtek wrote:
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.


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
Dec 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.