可以将 operator* 重载为多个 int 和 char* 吗?
Possible to overload operator* to multiple an int and a char*?
我想获得功能,以便我可以这样做:
std::cout << "here's a message" << 5*"\n";
我尝试了以下方法:
std::string operator* (int lhs, const char* rhs) {
std::string r = "";
for(int i = 0; i < lhs; i++) {
r += rhs;
}
return r;
}
我收到了这条错误信息:
error: ‘std::string operator*(int, const char*)’ must have an argument of class or enumerated type
根据此 SO post What does 'must have an argument of class or enumerated type' actually mean 中的答案,我似乎无法完成这段时间。真的是这样吗?如果没有,我该如何解决这个问题或安排解决方法?
我知道我能做的是将 rhs
作为 std::string
,但是整个练习的重点都已成定局,因为 5*std::string("\n")
非常笨重。
来自[over.oper]:
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.
所以你不能重载参数都是内置的运算符。此外,为了找到 operator*(int, std::string)
,它必须在 namespace std
中,并且向该命名空间添加定义是不正确的。
相反,您可以简单地提供一个小包装器:
struct Mult { int value; };
并为其提供重载:
std::string operator*(const Mult&, const char* );
std::string operator*(const char*, const Mult& );
来自 C++ 常见问题 here,
C++ language requires that your operator overloads take at least one
operand of a “class type” or enumeration type. The C++ language will
not let you define an operator all of whose operands / parameters are
of primitive types.
您既不能也不必重载该操作;
string ctor (2) 为您代劳
#include <iostream>
#include <string>
int main() {
std::cout << "here's a message:\n"
<< std::string(5, '\n')
<< "EOF" << std::endl;
}
输出:
here's a message:
EOF
您应该能够使用用户定义的文字来实现它。例如:
#include <iostream>
#include <string>
std::string operator"" _s(const char* s) { return std::string(s); }
std::string operator"" _s(const char* s, std::size_t len) { return std::string(s, len); }
std::string operator* (unsigned int k, std::string s) {
std::string t;
for (unsigned int i = 0; i < k; ++i)
t += s;
return t;
}
std::string operator* (std::string s, unsigned int k) { return k * s; }
int main() {
std::cout << "Jump!"_s * 5 << "\n";
}
我想获得功能,以便我可以这样做:
std::cout << "here's a message" << 5*"\n";
我尝试了以下方法:
std::string operator* (int lhs, const char* rhs) {
std::string r = "";
for(int i = 0; i < lhs; i++) {
r += rhs;
}
return r;
}
我收到了这条错误信息:
error: ‘std::string operator*(int, const char*)’ must have an argument of class or enumerated type
根据此 SO post What does 'must have an argument of class or enumerated type' actually mean 中的答案,我似乎无法完成这段时间。真的是这样吗?如果没有,我该如何解决这个问题或安排解决方法?
我知道我能做的是将 rhs
作为 std::string
,但是整个练习的重点都已成定局,因为 5*std::string("\n")
非常笨重。
来自[over.oper]:
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.
所以你不能重载参数都是内置的运算符。此外,为了找到 operator*(int, std::string)
,它必须在 namespace std
中,并且向该命名空间添加定义是不正确的。
相反,您可以简单地提供一个小包装器:
struct Mult { int value; };
并为其提供重载:
std::string operator*(const Mult&, const char* );
std::string operator*(const char*, const Mult& );
来自 C++ 常见问题 here,
C++ language requires that your operator overloads take at least one operand of a “class type” or enumeration type. The C++ language will not let you define an operator all of whose operands / parameters are of primitive types.
您既不能也不必重载该操作;
string ctor (2) 为您代劳
#include <iostream>
#include <string>
int main() {
std::cout << "here's a message:\n"
<< std::string(5, '\n')
<< "EOF" << std::endl;
}
输出:
here's a message:
EOF
您应该能够使用用户定义的文字来实现它。例如:
#include <iostream>
#include <string>
std::string operator"" _s(const char* s) { return std::string(s); }
std::string operator"" _s(const char* s, std::size_t len) { return std::string(s, len); }
std::string operator* (unsigned int k, std::string s) {
std::string t;
for (unsigned int i = 0; i < k; ++i)
t += s;
return t;
}
std::string operator* (std::string s, unsigned int k) { return k * s; }
int main() {
std::cout << "Jump!"_s * 5 << "\n";
}