由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++命名空间和算子重载
相关主题
C++ Q02:关于placement new
stl Compare为何需要重载()?大侠们救命, C++ operator new 问题
为什么不能成功排序求教new delete 在c++中的重载
针对class的成员变量的operator能重载吗?重载 ^ 操作符编译出错
C++: &*var是啥意思?make 时候遇到 undefined reference 怎么办?
C++糟粕和需要避免的。C++重载<<错误?
一个C++ operator new的重载问题一个matrix类定义的问题
C++ delete能否对某个库进行操作符重载?
相关话题的讨论汇总
话题: operator话题: namespace话题: n2话题: n1话题: class
进入Programming版参与讨论
1 (共1页)
w***g
发帖数: 5958
1
比较好奇这个。如果命名空间A和B都重载了算子+,那么编程的时候怎么确定用哪个+?
d******n
发帖数: 42
2
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;
: }

1 (共1页)
进入Programming版参与讨论
相关主题
能否对某个库进行操作符重载?C++: &*var是啥意思?
STL怎样同时重载()和< ?C++糟粕和需要避免的。
[合集] 再论auto_ptr/SmartPtr和内存泄漏一个C++ operator new的重载问题
C++里 怎么把1:10 定义为 1 2 3 4 5 6 7 8 9 10 一串数字?C++ delete
C++ Q02:关于placement new
stl Compare为何需要重载()?大侠们救命, C++ operator new 问题
为什么不能成功排序求教new delete 在c++中的重载
针对class的成员变量的operator能重载吗?重载 ^ 操作符编译出错
相关话题的讨论汇总
话题: operator话题: namespace话题: n2话题: n1话题: class