Im working my way through some C++ code and came across the following
void Classname::operator()()
{
//other code here
}
I assume this has something to do with overloading the constructor, but can someone elaborate on that?
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.
operator()is the function-call operator. It allows you to use a class instance like a function:This is useful for functors and various other C++ techniques. You can essentially pass a “function object”. This is just an object that has an overload of
operator(). So you pass it to a function template, who then calls it like it were a function. For example, ifClassname::operator()(int)is defined:This will call
instance‘soperator()(int)member for each integer in the list. You can have member variables in theinstanceobject, so thatoperator()(int)can do whatever processing you require. This is more flexible than passing a raw function, since these member variables are non-global data.