P
Piotr Wyderski
Hello,
is it possible to reuse a friend operator which is defined
inside a class? I'd like to obtain the following behaviour:
class integer {
[...]
integer operator +(signed long int v) const {
// It somehow does its job
}
friend inline integer operator +(signed long int lhs, const integer&
rhs) {
return rhs.operator +(lhs); // This is allowed because of
commutativity
}
friend inline integer operator +(signed int lhs, const integer& rhs) {
return operator +(static_cast<signed long int>(lhs),rhs); // Here's
the problem
}
};
integer i;
i + 10; // the first operator is used
10L + i; // the second operator is used
10 + i; // the third is used and then control is passed to the second one
The problem is that the compiler (G++ 3.4) doesn't see the operators
declared as friends. It displays a message that no operator can be matched
and shows the list of alternatives, but only local operators are listed (in
this example there's only one, in the real project there are 6). When the
friend operators are declared in a standard way, i.e. outside of the class,
everything works OK. So the question is: how to qualify the "operator +()"
in the third operator's body to use the second one? None of these
ad hoc approaches works:
"return :perator +("
"return integer:perator +("
"return friend operator +("
Best regards
Piotr Wyderski
is it possible to reuse a friend operator which is defined
inside a class? I'd like to obtain the following behaviour:
class integer {
[...]
integer operator +(signed long int v) const {
// It somehow does its job
}
friend inline integer operator +(signed long int lhs, const integer&
rhs) {
return rhs.operator +(lhs); // This is allowed because of
commutativity
}
friend inline integer operator +(signed int lhs, const integer& rhs) {
return operator +(static_cast<signed long int>(lhs),rhs); // Here's
the problem
}
};
integer i;
i + 10; // the first operator is used
10L + i; // the second operator is used
10 + i; // the third is used and then control is passed to the second one
The problem is that the compiler (G++ 3.4) doesn't see the operators
declared as friends. It displays a message that no operator can be matched
and shows the list of alternatives, but only local operators are listed (in
this example there's only one, in the real project there are 6). When the
friend operators are declared in a standard way, i.e. outside of the class,
everything works OK. So the question is: how to qualify the "operator +()"
in the third operator's body to use the second one? None of these
ad hoc approaches works:
"return :perator +("
"return integer:perator +("
"return friend operator +("
Best regards
Piotr Wyderski