w***g 发帖数: 5958 | 1 比较好奇这个。如果命名空间A和B都重载了算子+,那么编程的时候怎么确定用哪个+? |
d******n 发帖数: 42 | |
d******n 发帖数: 42 | 3 I think the compiler will report error...
Ambiguous operator... |
H***a 发帖数: 735 | 4 在class里面重载就没问题了
【在 w***g 的大作中提到】 : 比较好奇这个。如果命名空间A和B都重载了算子+,那么编程的时候怎么确定用哪个+?
|
k**f 发帖数: 372 | 5
Only user defined class can have overloaded operator. The compiler will try
to resolve the operator based on the type of the operand. If you have two
classes of the same name but in different namespace, then you need to
specify which class the object is when it is declared. I don't see how you
can overload an operator with the same signature unless you do it on purpose.
Also related is the special "Koenig lookup".
http://en.wikipedia.org/wiki/Argument_dependent_name_lookup
If you have a specifi
【在 w***g 的大作中提到】 : 比较好奇这个。如果命名空间A和B都重载了算子+,那么编程的时候怎么确定用哪个+?
|
w***g 发帖数: 5958 | 6 自己动手试了一下,发现用下面的方法可以解决这个问题。
#include
using namespace std;
class A {
};
namespace n1 {
const A& operator ++ (A &a) {
cout << "n1" << endl;
return a;
}
}
namespace n2 {
const A& operator ++ (A &a) {
cout << "n2" << endl;
return a;
}
}
using namespace n1;
using namespace n2;
int main ()
{
A a;
n1::operator ++(a);
n2::operator ++(a);
return 0;
}
【在 w***g 的大作中提到】 : 比较好奇这个。如果命名空间A和B都重载了算子+,那么编程的时候怎么确定用哪个+?
|
k**f 发帖数: 372 | 7
Yes, you can do this, but why?
【在 w***g 的大作中提到】 : 自己动手试了一下,发现用下面的方法可以解决这个问题。 : #include : using namespace std; : class A { : }; : namespace n1 { : const A& operator ++ (A &a) { : cout << "n1" << endl; : return a; : }
|