I have my own class where I define and use << operator like so:
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;
...
The number of chain << calls is different every time. These numbers together represent a code and I need to do something when the code is complete.
Do I have any other options to know when it's complete rather than adding a special terminating value to each chain, like below?
inst << a << b << c << d << term;
...
inst << a << b << term;
EDIT2 : current solution following LogicStuff answers:
--- 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;
}
This is the output:
main start
chain_constructor
cutter_constructor
1
2
3
cutter_destructor
-----
cutter_constructor
4
5
cutter_destructor