相同类型的C++ <<运算符重载
C++ <<operator overloading with same type
我正在写一个方法来打印std::cout
中的一些空间,我知道还有其他方法可以使用标准库来实现同样的目标。无论如何,我使用了一个typedef
来存储空间的数量和<<
运算符的重载。但是我的重载根本没有被调用,因为我的typedef
被解释为一个无符号的int
那么,我该如何告诉编译器调用我的函数呢?
class MyClass {
private:
typedef unsigned int space_type;
public:
std::ostream& operator<< (std::ostream& _os, space_type _n_spaces) {
for (int _C = 0; _C < _n_spaces; _C++)
_os << " ";
return _os;
}
void PrintNumbers(char _a, char _b) {
space_type spaces = 5;
std::cout << _a << spaces << _b << std::endl;
}
}
int main () {
MyClass class_instance;
class_instance.PrintNumbers('K', 'X');
std::cin.get();
return 0;
}
这是预期输出:
K X
这是我获得的输出:
K5X // 5 is interpreted as an unsigned int, so my overloaded function
// isn't called, instead is called the std overloading with unsigned int
Typedef不创建新类型,它只创建现有类型的别名。你可能会使用这样的东西:
struct SpaceType {
int space_cnt;
};
...
std::ostream& operator<< (std::ostream& _os, SpaceType _n_spaces) {
for (int _C = 0; _C < _n_spaces.space_cnt; _C++)
_os << " ";
return _os;
}
...
SpaceType spaces = { 5 };
std::cout << _a << spaces << _b << std::endl;
由于您将space_type
定义为别名(即typedef)而不是类型,因此它与int
无法区分,如果您试图重载operator(std::ostream&, int)
,编译器将发出错误。
但你正在做的是定义一个类成员:
std::ostream& operator<< (std::ostream& _os, space_type _n_spaces)
当您将运算符定义为类成员时,运算符的第一个参数是(隐式)类的实例。因此,原则上,这只能用调用
MyClass m;
m << ???
但这里有一个问题:使用中缀表示法调用的运算符函数只能有两个参数,而在成员运算符函数的情况下,第一个参数是隐式的。m << x
只能由MyClass::operator<<(decltype(x))
来实现。
简而言之,您只能使用非成员operator<<
来实现这一点,并且该重载的第二个参数必须是用户类型。因此以下操作会很好:
struct space_t {
unsigned x;
space_t(unsigned x) : x(x) {}
operator unsigned() const { return x; }
};
std::ostream& operator<< (std::ostream& os, space_t n) {
for (unsigned i = 0; i < n; ++i) os << " ";
return os;
}
在ideeone 上查看
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何显式调用运算符<<
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 过载输出<<用于类的运算符,以打印其中的元组
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<:此运算符函数的参数太多
- C++继承运算符<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 为什么字符串流运算符<<擦除原始值
- 关于使用运算符<<为新手提供C++中的模板
- 我已经完成了<<运算符重载,但它'It’不起作用
- 重载运算符<<输出地址而不是数据成员
- 错误:没有匹配'运算符<<"在'std::cout
- 重载运算符<<用于ostream语法
- 当运算符<存在时,为什么要定义 LT?
- log4cxx访问异常,使用<<运算符和宏
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多