So basically this is what I want to make:
void someMethod() {
StreamClass streamClass{};
streamClass << "Something" << "\n"; //do something
streamClass.doA << "Something" << "\n"; //do another thing
streamClass.doB << "Something" << "\n"; //do something else
//If the first one is impossible, what about this?
streamClass << "Something" << "\n"; //do something
streamClass::doA << "Something" << "\n"; //do another thing
streamClass::doB << "Something" << "\n"; //do something else
//Or this?
streamClass<enumA> << "Something" << "\n"; //do something
streamClass<enumB> << "Something" << "\n"; //do another thing
streamClass<enumC> << "Something" << "\n"; //do something else
}
Header file:
class StreamClass {
public:
StreamClass(); //Init class
//... and many other things
template<typename T>
StreamClass& operator<<(T t) {
//... do something that uses data and t
}
//And do something here?
protected:
Data data; //some data owned by this object
};
So, is there anyway to do something like that? And how to do it in the fastest way?
The first way is perfectly viable, as long as you make
doAanddoBmembers ofStreamClass, give them a reference to theStreamClassinstance that has them (during construction) and make them implementoperator<<.At the end, the types of
doAanddoBshould hold a reference/pointer toStreamClassorStreamClass::Data. Consider forbidding copy/move constructors and assignment operators.The second option is syntactically valid, but requires
doAanddoBto be static, which ultimately makes it impossible to have twoStreamClassinstances at the same time with different behavior.The last way is syntactically invalid, given that
streamClassis not a variable template.