我有自己的类,在其中定义和使用<<运算符,如下所示:
vw& vw::operator<<(const int &a) {
// add a to an internal buffer
// do some work when it's the last number
return *this;
}
...
vw inst;
inst << a << b << c << d;
...
inst << a << b;
...
每次调用<<的次数都不相同。这些数字共同代表一个代码,代码完成后,我需要做些事情。
我还有其他选择可以知道何时完成,而不是像下面这样向每个链添加特殊的终止值吗?
inst << a << b << c << d << term;
...
inst << a << b << term;
EDIT2 :根据 LogicStuff 的当前解决方案:
--- chain.h ---
#pragma once
#include <iostream>
class chain
{
public:
chain();
~chain();
};
--- chain_cutter.h ---
#pragma once
#include "chain.h"
class chain_cutter
{
chain &inst;
public:
explicit chain_cutter(chain &inst) : inst(inst) {
std::cout << "cutter_constructor" << std::endl;
}
~chain_cutter();
};
--- chain_cutter.cpp ---
#include "stdafx.h"
#include "chain_cutter.h"
chain_cutter::~chain_cutter()
{
std::cout << "cutter_destructor" << std::endl;
}
--- chain.cpp ---
#include "stdafx.h"
#include "chain.h"
chain::chain()
{
std::cout << std::endl << "chain_constructor" << std::endl;
}
chain::~chain()
{
std::cout << std::endl << "chain_destructor" << std::endl;
}
--- flowchart.cpp ---
#include "stdafx.h"
#include <iostream>
#include "chain.h"
#include "chain_cutter.h"
chain_cutter operator<<(chain &inst, const int &a) {
chain_cutter cutter(inst);
std::cout << a << std::endl;
return cutter;
}
chain_cutter&& operator<<(chain_cutter &&cutter, const int &a) {
std::cout << a << std::endl;
return std::move(cutter);
}
int main()
{
std::cout << "main start" << std::endl;
chain ch;
ch << 1 << 2 << 3;
std::cout << std::endl << "-----" << std::endl;
ch << 4 << 5;
return 0;
}
这是输出:
main start
chain_constructor
cutter_constructor
1
2
3
cutter_destructor
-----
cutter_constructor
4
5
cutter_destructor
答案 0 :(得分:6)
可以不更改当前语法。
您将必须使inst << a
(即当前的operator<<
)返回“特殊”类的临时实例,并保留对inst
的引用,并实现operator<<
通过调用inst.operator<<
,返回引用
到*this
,然后在其析构函数中执行额外的工作,该析构函数将在语句末尾调用。
是的,您可以使用它来跟踪通话计数。
我建议这些nonmember operator<<
重载(vw_chain
是新的代理类):
// Left-most operator<< call matches this
vw_chain operator<<(vw &inst, const int &a) {
return vw_chain(inst, a);
}
// All subsequent calls within the << chain match this
vw_chain &&operator<<(vw_chain &&chain, const int &a) {
chain.insert(a);
return std::move(chain);
}
课程本身:
struct vw_chain
{
explicit vw_chain(vw &inst, const int &a) :
inst(inst)
{
insert(a);
}
~vw_chain() {
// do something
}
void insert(const int &a) {
// This, the original operator<<, should be made accessible only to this
// function (private, friend class declaration?), not to cause ambiguity.
// Or, perhaps, put the implementation of the original operator<< here
// and remove it altogether.
inst << a;
++insertion_count;
}
vw &inst;
size_t insertion_count = 0;
};
我们必须通过右值引用传递实例。我们在vw_chain
的构造函数中进行第一次插入,以获取mandatory copy elision(C ++ 17),该函数仅适用于prvalues。 NRVO和较旧的标准未指定是否在return
语句中完成复制。我们不应该依赖于此。
C ++ 17之前的解决方案:
struct vw_chain
{
// We keep the constructor simpler
vw_chain(vw &inst) : inst(inst) {}
// Moved-from chains are disabled
vw_chain(vw_chain &&other) :
inst(other.inst),
insertion_count(other.insertion_count) {
other.is_enabled = false;
}
// And will not call the termination logic
~vw_chain() {
if(is_enabled) {
// do something
}
}
void insert(const int &a) {
inst << a;
++insertion_count;
}
vw &inst;
size_t insertion_count = 0;
bool is_enabled = true;
};
// The first overload changes to this
vw_chain operator<<(vw &inst, const int &a) {
vw_chain chain(inst);
chain.insert(a);
return chain;
}
答案 1 :(得分:5)
您可以:
使用特殊的最终类型来表示终止(la std::endl
)。
避免使用operator<<
,而是定义一个可变参数模板函数。可变参数模板函数支持任意数量的任意参数,并在编译时知道该数量。
将逻辑放入vw
的析构函数中,并仅在右值上允许operator<<
。例如
vw{} << a << b << c;
// `vw::~vw()` contains termination logic