I was experimenting with new overloaded operators, I have created one void opera
ID: 653053 • Letter: I
Question
I was experimenting with new overloaded operators, I have created one void operator and another one that returns something when it's called:
#include <iostream>
struct chichachicha{
int operator()(){
return 25;
};
};
struct boomboom{
void operator()() {
std::cout << "Hi folks!" << std::endl;
};
};
int main(){
chichachicha objC;
int f = objC();
std::cout << f << std::endl;
boomboom objB;
objB();
return(0);
}
Is this the best way to write a code that does that?
How can I write an operator that can be called from anywhere in the code without instantiating the associated struct, that returns void/nothing and only prints a string of text? I'm not considering static methods, just operators.
Explanation / Answer
Despite "I'm not considering static methods, just operators", the statement immediately before it suggests you are asking for a static operator(). If this were possible, the most vexing parse would be much more vexing!
Upon further inspection, it reads like you are basically in the market for a static function. The whole purpose of operator() is to make a functor. If you want a polymorphic version of operator(), I'm afraid you'll have to instantiate something somewhere.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.