如何使用运算符在同一行中多次调用函数

How to call a function several times in the same line using an operator

本文关键字:一行 函数 调用 运算符 何使用      更新时间:2023-10-16

此函数将TElement添加到vector<TElement>

friend void operator<<(ToDo& todu, const ElementT& D) {
todu.add(D);
return;
}

我想这样称呼它:

todo << elem1 << elem2 << elem3 << elem4...

如果你想像这样链接操作,那么你可以简单地通过引用返回ToDo参数,如下所示:

friend ToDo& operator<<(ToDo& todu, const ElementT& D) 
{
todu.add(D);
return todu;
}

现在你应该能够像这样使用operator<<

todo << elem1 << elem2 << elem3 << elem4;