C++ C++;运算符重载会影响类运算符函数
如果我过载了C++ C++;运算符重载会影响类运算符函数,c++,operator-overloading,C++,Operator Overloading,如果我过载了=运算符并使用类=在另一个操作符重载中,它接受非重载还是重载?我正在尝试创建一个noob_ptr(我想到的一种自定义指针包装器) 下面的示例是否取消了在我的类中使用重载运算符 class noob_ptr { protected: //or public bool operator,(noob_ptr y) //yes, z is also a noob_ptr { ... if(y!=z)...
=代码>运算符并使用类=
在另一个操作符重载中,它接受非重载还是重载?我正在尝试创建一个noob_ptr(我想到的一种自定义指针包装器)
下面的示例是否取消了在我的类中使用重载运算符
class noob_ptr
{
protected: //or public
bool operator,(noob_ptr y) //yes, z is also a noob_ptr
{
...
if(y!=z)...
...
}
...
private:
bool operator!=(noob_ptr x)
{
...
}
}
如果您提供的操作数是语言内置的操作数=代码>将起作用,然后就可以使用它了。如果它们是用户定义的类型,那么它将搜索用户定义的操作符=为这些类型(或支持从这些类型隐式转换的某些类型)定义的代码>。如果您提供的操作数是语言内置的操作数=代码>将起作用,然后就可以使用它了。如果它们是用户定义的类型,那么它将搜索用户定义的操作符=为这些类型定义的代码>(或支持从这些类型隐式转换的某些类型)。如果z
的类型也是noob_ptr
,那么答案是肯定的,它将调用重载的操作符=代码>用于您的类。
另外,我建议您使用以下比较方法签名:
bool operator != (const noob_ptr& x) const;
因此,它可以用于常量指针,也可以在调用重载运算符时避免对象复制
UPD:如果您声明操作员=
作为私有,则它将在noob_ptr
类的所有成员函数、朋友类和noob_ptr
类的函数中可用,并且对于所有其他用途,将导致编译错误,并显示一条消息,如:“operator!=由于其保护级别而无法访问”如果z
的类型也是noob_ptr
,那么答案是肯定的,它将调用重载的操作符=代码>用于您的类。
另外,我建议您使用以下比较方法签名:
bool operator != (const noob_ptr& x) const;
因此,它可以用于常量指针,也可以在调用重载运算符时避免对象复制
UPD:如果您声明操作员=
作为私有,那么它将在noob_ptr
类的所有成员函数、朋友类和noob_ptr
类的函数中可用,并且对于所有其他用途,它将导致编译错误,并显示一条消息,如:“operator!=由于其保护级别而无法访问”C++将始终使用“最佳匹配”在限定符、访问说明符(如private、scope、namespace等)中,哪一个最接近
因此,如果存在全局命名空间运算符!=类1(缺少左侧参数,假定为类&或const类&如果方法是const——应该是),那么在类(名称空间)内您将得到类内的参数
如果在全局名称空间和类之间只有一个名称空间,那么显然会得到该名称空间
下面的代码演示了全局范围和类范围之间的关系。您可以通过添加常量限定符等来扩展它
#include <iostream>
using namespace std;
// Forward declaration to use without the class definition
class X;
bool operator!=( int lhs, const X& rhs) {
cout << "bool operator!=( int lhs, const X& rhs)" << endl;
return false;
}
bool operator!=(const X& lhs, const X& rhs) {
cout << "bool operator!=(const X& lhs, const X& rhs)" << endl;
return false;
}
bool operator!=(const X& lhs, int rhs) {
cout << "bool operator!=(const X& lhs, int rhs)" << endl;
return false;
}
// Note: Can't do: bool operator!=(int lhs, int rhs) -- already defined
class X {
private:
int x;
public:
X(int value) : x(value) { }
bool operator !=(const X& rhs) {
cout << "bool X::operator !=(const X& rhs)" << endl;
return true;
}
bool operator !=(int rhs) {
cout << "bool X::operator !=(int rhs)" << endl;
return true;
}
void f() {
X compare(1);
cout << "X::f()" << endl;
cout << (5 != 3) << endl; // Uses built-in
cout << (*this != 3) << endl; // Uses member function
cout << (*this != 1) << endl; // Uses member function
cout << (1 != *this) << endl; // There can be no member function, uses global namespace
cout << (*this != compare) << endl;
}
};
void f(const X& arg) {
cout << "f()" << endl;
X compare(1);
cout << (5 != 3) << endl; // Uses built in
cout << (arg != 3) << endl; // Uses global namespace
cout << (arg != 1) << endl; // Uses global namespace
cout << (1 != arg) << endl; // Uses global namespace
cout << (arg != compare) << endl; // Uses global namespace
}
int main(int argc, char **argv) {
X x(1);
x.f();
f(x);
return 0;
}
#包括
使用名称空间std;
//转发声明以在不使用类定义的情况下使用
X类;
接线员=(内部左侧、常量X和右侧){
coutC++将始终使用“最佳匹配”,它是最接近的限定符、访问说明符,如private、scope、namespace等
因此,如果有一个全局名称空间操作符!=和一个类one(它缺少假定为类的左侧参数&或const类&如果方法是const——它应该是),那么在类(名称空间)内部,您将得到类内部的参数
如果在全局名称空间和类之间只有一个名称空间,那么显然会得到该名称空间
下面的代码演示了全局作用域和类作用域之间的关系
#include <iostream>
using namespace std;
// Forward declaration to use without the class definition
class X;
bool operator!=( int lhs, const X& rhs) {
cout << "bool operator!=( int lhs, const X& rhs)" << endl;
return false;
}
bool operator!=(const X& lhs, const X& rhs) {
cout << "bool operator!=(const X& lhs, const X& rhs)" << endl;
return false;
}
bool operator!=(const X& lhs, int rhs) {
cout << "bool operator!=(const X& lhs, int rhs)" << endl;
return false;
}
// Note: Can't do: bool operator!=(int lhs, int rhs) -- already defined
class X {
private:
int x;
public:
X(int value) : x(value) { }
bool operator !=(const X& rhs) {
cout << "bool X::operator !=(const X& rhs)" << endl;
return true;
}
bool operator !=(int rhs) {
cout << "bool X::operator !=(int rhs)" << endl;
return true;
}
void f() {
X compare(1);
cout << "X::f()" << endl;
cout << (5 != 3) << endl; // Uses built-in
cout << (*this != 3) << endl; // Uses member function
cout << (*this != 1) << endl; // Uses member function
cout << (1 != *this) << endl; // There can be no member function, uses global namespace
cout << (*this != compare) << endl;
}
};
void f(const X& arg) {
cout << "f()" << endl;
X compare(1);
cout << (5 != 3) << endl; // Uses built in
cout << (arg != 3) << endl; // Uses global namespace
cout << (arg != 1) << endl; // Uses global namespace
cout << (1 != arg) << endl; // Uses global namespace
cout << (arg != compare) << endl; // Uses global namespace
}
int main(int argc, char **argv) {
X x(1);
x.f();
f(x);
return 0;
}
#包括
使用名称空间std;
//转发声明以在不使用类定义的情况下使用
X类;
布尔运算符!=(内部左侧、常量X和右侧){
我不能完全理解你的意思吗,但是用操作符==
实现操作符是很常见的:bool操作符!=(constmyclass&lhs,constmyclass&rhs){return!(lhs==rhs);}
我的意思是,在课堂上,我使用了许多重载。如果在中使用,它们会改变其他重载的行为吗?你的问题不清楚。也许你可以发布一个演示你的问题的帖子。请参阅下面我的答案和会员访问限制。简言之,第二个问题的答案是否定的。你到底为什么需要重载操作符,
?为了给其他人带来愉快的调试?我不完全明白你的意思,但是实现操作符!=
在操作符==
:bool操作符!=(const MyClass&lhs,const MyClass&rhs){return!(lhs==rhs);}
我的意思是,在课堂上,我使用了许多重载。如果在中使用,它们会改变其他重载的行为吗?你的问题不清楚。也许你可以发布一个演示你的问题的帖子。请参阅下面我的答案和会员访问限制。简言之,第二个问题的答案是否定的。你到底为什么需要重载operator,
?为了给别人一个愉快的调试?我用一些代码更新了这个问题。你是说我的重载是主要目标还是次要目标?@tuğrulbüyükıkık:是的,假设y
和z
都有类型noobúptr
,那么如果(y!=z)
将使用noob ptr::operator!=(noob ptr)
进行比较(如果存在全局bool操作符!=(noob_ptr a,noob_ptr b)
)。我用一些代码更新了这个问题。你说我的重载是主要目标还是次要目标?@tuğrulbüyıkık:是的,假设y
和z
都有类型noob u ptr
,那么如果(y!=z)
将使用noob_ptr::operator!=(noob_ptr)
进行比较(如果存在)