C++:如何为 char 数组创建比较函数?
这可能吗?当我将 char 作为类型时,我收到奇怪的错误消息:
inline bool operator==(const char *str1, const char *str2){
// ...
}
错误消息:错误 C2803:'operator ==' 必须至少有一个类类型的形式参数
...我根本不明白。
我在想我是否可以直接比较像:
const char *str1 = "something";
const char *str2 = "something else";
const char str3[] = "lol"; // not sure if this is same as above
然后比较:
if(str1 == str2){
// ...
}
等的东西。
但我也希望它能够与:
char *str = new char[100];
和:
char *str = (char *)malloc(100);
我假设我用这种方式使用的每个字符数组都会以NULL字符结尾,所以检查应该是可能的,但我知道这可能不安全等。我只是想知道这是否可以做到,以及如何做到。
Is this possible? i get weird error message when i put char as the type:
inline bool operator==(const char *str1, const char *str2){
// ...
}
Error message: error C2803: 'operator ==' must have at least one formal parameter of class type
... which i dont understand at all.
I was thinking if i could directly compare stuff like:
const char *str1 = "something";
const char *str2 = "something else";
const char str3[] = "lol"; // not sure if this is same as above
and then compare:
if(str1 == str2){
// ...
}
etc.
But i also want it to work with:
char *str = new char[100];
and:
char *str = (char *)malloc(100);
I am assuming every char array i use this way would end in NULL character, so the checking should be possible, but i understand it can be unsafe etc. I just want to know if this is possible to do, and how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是不可能的。正如编译器所指出的,您不能为原始数据类型重载此运算符。比较的至少一侧必须是非原始的,才能实现重载。
同样的意义,您不能从原始数据类型派生新类(以向其添加功能)。
It is not possible. As your compiler points out, you cannot overload this operator for primitive data types. At least one side of the comparison must be non-primitive for the overload to be possible.
In the same sense, you cannot derive a new class from a primitive data type (to add functionality to it).
您正在尝试比较两个指针。
但是,您已经提供了 C++ 标记,因此您应该使用
std::string
:You are attempting to compare two pointers.
But, you've provided the C++ tag, so you should be using
std::string
:指针是内置类型。它们已经有内置的比较运算符,您无法覆盖它们。只需使用 std::string 即可。
Pointers are built-in types. There are built-in comparison operators for them already, you cannot override them. Just use
std::string
.您当然完全可以编写自己的比较函数,事实上这是要做的最基本的事情之一。我不知道为什么有人会说你不能。您只是不能将其称为
operator =
。这是(未经测试):You most certainly, totally can write your own comparison function, and in fact it's one of the most basic things to do. I have no idea why someone would say that you can't. You just can't call it
operator =
. Here it is (untested):使用
strcmp()
比较字符:它适用于字符指针char *
和字符数组char [ ]
。Use
strcmp()
to compare chars: It works on char pointerschar *
and char arrayschar [ ]
.决定我的评论作为答案会更好,您应该使用标准 string 函数为此(strncmp、strncat 等)。
编辑:
正如另一个答案所指出的,你不能超载。但对于 char 数组和 char 指针,您应该使用标准库函数。
Decided that my comment would be better as an answer, you should use the standard string functions for this (strncmp, strncat, etc).
Edit:
As pointed out in another answer, you can't do the overload. But in the case of the char arrays and char pointers you should use the standard library functions.