结构指针的运算符重载
Operator Overloading for struct pointers
我正在尝试使用运算符重载来比较两个动态分配的结构指针。据我了解,运算符重载应该使用按引用传递,那么这是否意味着重载 == 运算符来实现此特定目标是不可能的?
目标是在不编辑 main() 中的代码的情况下打印出 Success
#include <iostream>
struct VAR
{
int x;
VAR()
{
x = 0;
}
VAR(int val)
{
x = val;
}
bool operator==(VAR &lhs)
{
std::cout << "operator called\n";
return (x == lhs.x);
}
bool operator==(VAR *lhs)
{
std::cout << "ptr operator called\n";
return (x == lhs->x);
}
};
int main()
{
VAR *v1 = new VAR(5);
VAR *v2 = new VAR(5);
if (v1 == v2)
std::cout << "Success\n";
else
std::cout << "Fail\n";
delete v1;
delete v2;
v1 = nullptr;
v2 = nullptr;
system("pause");
return 0;
}
考虑以下代码,与您的几乎相同但没有指针:
VAR v1(5);
VAR v2(5);
if (v1 == v2) { ... }
该条件实际上由编译器处理为
if (v1.operator==(v2)) { ... }
当您将 v1
转换为指针时,编译器不会自动解除对指针的引用,即它不会
v1->operator==(v2)
简单的解决方案是自己取消引用指针:
*v1 == *v2
== 运算符的常见实现是
bool operator==(const VAR & lhs)
即使您将一个点分配给 VAR 对象,您也可以 "call" 使用 * 运算符的 == 运算符。
VAR *v1 = new VAR(5);
VAR *v2 = new VAR(5);
if (*v1 == *v2)
std::cout << "Success\n";
else
std::cout << "Fail\n";
你的==
比较两个指针。指针是基本类型,不管它们指向什么,you cannot overload operators when all operands are of primitive types.
换句话说:如果左操作数是 VAR
而不是 VAR*
,您的重载运算符就可以工作。
当然,无论如何,您的代码并不能令人信服地使用指针或动态内存分配。只需删除所有这些:
int main()
{
VAR v1(5);
VAR v2(5);
if (v1 == v2)
std::cout << "Success\n";
else
std::cout << "Fail\n";
}
这样,您也不再需要第二个操作员了。为了完整起见,您可以这样调用它:
int main()
{
VAR v1(5);
VAR v2(5);
if (v1 == &v2)
std::cout << "Success\n";
else
std::cout << "Fail\n";
}
#include <iostream>
struct VAR
{
int x;
VAR()
{
x = 0;
}
VAR(int val)
{
x = val;
}
bool operator==(VAR &lhs)
{
std::cout << "operator called\n";
return (x == lhs.x);
}
bool operator==(VAR *lhs)
{
std::cout << "ptr operator called\n";
return (x == lhs->x);
}
};
int main()
{
VAR *v1 = new VAR(5);
VAR *v2 = new VAR(5);
if (v1->operator==(v2))
std::cout << "Success\n";
else
std::cout << "Fail\n";
delete v1;
delete v2;
v1 = nullptr;
v2 = nullptr;
return 0;
}
为了完成其他答案,这里是 C++ 标准中明确禁止使用两个指针重载运算符的部分(强调我的):
An operator function shall either be a non-static member function or
be a non-member function that has at least one parameter whose type is
a class, a reference to a class, an enumeration, or a reference to an
enumeration. It is not possible to change the precedence, grouping, or
number of operands of operators. The meaning of the operators
= , (unary) & , and , (comma), predefined for each type, can be changed for specific class and enumeration types by defining operator
functions that implement these operators. Operator functions are
inherited in the same manner as other base class functions.
这解释了为什么您不能在不更改 main
函数中的代码的情况下使用重载的 operator==
。
我正在尝试使用运算符重载来比较两个动态分配的结构指针。据我了解,运算符重载应该使用按引用传递,那么这是否意味着重载 == 运算符来实现此特定目标是不可能的?
目标是在不编辑 main() 中的代码的情况下打印出 Success
#include <iostream>
struct VAR
{
int x;
VAR()
{
x = 0;
}
VAR(int val)
{
x = val;
}
bool operator==(VAR &lhs)
{
std::cout << "operator called\n";
return (x == lhs.x);
}
bool operator==(VAR *lhs)
{
std::cout << "ptr operator called\n";
return (x == lhs->x);
}
};
int main()
{
VAR *v1 = new VAR(5);
VAR *v2 = new VAR(5);
if (v1 == v2)
std::cout << "Success\n";
else
std::cout << "Fail\n";
delete v1;
delete v2;
v1 = nullptr;
v2 = nullptr;
system("pause");
return 0;
}
考虑以下代码,与您的几乎相同但没有指针:
VAR v1(5);
VAR v2(5);
if (v1 == v2) { ... }
该条件实际上由编译器处理为
if (v1.operator==(v2)) { ... }
当您将 v1
转换为指针时,编译器不会自动解除对指针的引用,即它不会
v1->operator==(v2)
简单的解决方案是自己取消引用指针:
*v1 == *v2
== 运算符的常见实现是
bool operator==(const VAR & lhs)
即使您将一个点分配给 VAR 对象,您也可以 "call" 使用 * 运算符的 == 运算符。
VAR *v1 = new VAR(5);
VAR *v2 = new VAR(5);
if (*v1 == *v2)
std::cout << "Success\n";
else
std::cout << "Fail\n";
你的==
比较两个指针。指针是基本类型,不管它们指向什么,you cannot overload operators when all operands are of primitive types.
换句话说:如果左操作数是 VAR
而不是 VAR*
,您的重载运算符就可以工作。
当然,无论如何,您的代码并不能令人信服地使用指针或动态内存分配。只需删除所有这些:
int main()
{
VAR v1(5);
VAR v2(5);
if (v1 == v2)
std::cout << "Success\n";
else
std::cout << "Fail\n";
}
这样,您也不再需要第二个操作员了。为了完整起见,您可以这样调用它:
int main()
{
VAR v1(5);
VAR v2(5);
if (v1 == &v2)
std::cout << "Success\n";
else
std::cout << "Fail\n";
}
#include <iostream>
struct VAR
{
int x;
VAR()
{
x = 0;
}
VAR(int val)
{
x = val;
}
bool operator==(VAR &lhs)
{
std::cout << "operator called\n";
return (x == lhs.x);
}
bool operator==(VAR *lhs)
{
std::cout << "ptr operator called\n";
return (x == lhs->x);
}
};
int main()
{
VAR *v1 = new VAR(5);
VAR *v2 = new VAR(5);
if (v1->operator==(v2))
std::cout << "Success\n";
else
std::cout << "Fail\n";
delete v1;
delete v2;
v1 = nullptr;
v2 = nullptr;
return 0;
}
为了完成其他答案,这里是 C++ 标准中明确禁止使用两个指针重载运算符的部分(强调我的):
An operator function shall either be a non-static member function or be a non-member function that has at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. It is not possible to change the precedence, grouping, or number of operands of operators. The meaning of the operators = , (unary) & , and , (comma), predefined for each type, can be changed for specific class and enumeration types by defining operator functions that implement these operators. Operator functions are inherited in the same manner as other base class functions.
这解释了为什么您不能在不更改 main
函数中的代码的情况下使用重载的 operator==
。