devxlogo

Object Functions in Use

An object function is a regular C++ object whose class defines the () operator. An object with a () operator can be called like a function:

CFoo foo; // just an object whose class has operator() definedfoo(); // like a function; makes operator() work

You can use an object function to find the first occurence of a string started by a given symbol in a vector:

#include #include #include #include using namespace std;class starts {	char ch;public:	starts(char c) : ch(c) {}	bool operator() (const string &s) {		return s.size() && s[0] == ch;	}};int main(){	vector vec(3); // vector of three elements	vec[0] = "sir";	vec[1] = "zebra";	vec[2] = "home";	vector::iterator iter =		find_if(vec.begin(), vec.end(), starts('z'));	cout << *iter << "
";	return 0;}

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Engineering Leaders Spot Weak Proposals

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.