C++ 使用运算符转发整个类
我想知道。我遇到了一个问题,这里是一个小报告。基本上,我想把一切都转发出去。问题是,使用第一个 如果这样做,则也可以对C++ 使用运算符转发整个类,c++,templates,C++,Templates,我想知道。我遇到了一个问题,这里是一个小报告。基本上,我想把一切都转发出去。问题是,使用第一个 如果这样做,则也可以对const对象调用此函数: void f(const FowardIt &o)//note: inside the function, o is an const object! { o << 1; o << SomeUserStruct(); } void f(const FowardIt&o)//注意:在函数中,o是一个con
const
对象调用此函数:
void f(const FowardIt &o)//note: inside the function, o is an const object!
{
o << 1;
o << SomeUserStruct();
}
void f(const FowardIt&o)//注意:在函数中,o是一个const对象!
{
o回答得很好。但是如果我使方法常量,我也必须使返回常量。这将是不合逻辑的(对于常规流)。@acidzombie24:是的。我也添加了这个!你知道这有什么问题吗?十六进制工作,所以我们知道函数正在向前。但是endl不是。
template<typename T> FowardIt& operator<<(const T&t)
//^^^^^ put const here
template<typename T>
const FowardIt& operator<<(const T&t) const
^^^^^ ^^^^^ ^^^^^
| | |
| | put const here as well
| put const here
|
You've to make the return-type also const
since it can't return non-const reference anymore
void f(const FowardIt &o)//note: inside the function, o is an const object!
{
o << 1;
o << SomeUserStruct();
}