in c++ i have following code
class Foobar{
public:
Foobar * operator()(){
return new Foobar;
};
My quesion is how to call the ();
if i do Foobar foo() the constructor gets called
i am confused about behaviour of ()
can some explain me
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While GMan’s answer is factually correct, you should never overload an operator to do something unexpected – this goes against all good-practice programming rules. When a user reads code he expects operators to behave in some way, and making them behave differently is good only for obfuscating coding competitions.
The
()operator in C++ can be used to make an object represent a function. This actually has a name – it’s called functor, and is used extensively in the STL to provide generic algorithms. Google for stl functor to learn about a good usage of the technique.