function object
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
for example. when I write ltstr() ,what happen? first I guess ltstr constructor called ,second operator()(function) called. do I guess right?
1) why don't you try and compile it and see what happens?
2) why would you do something like that? It solves nothing and complicates everything.
2) why would you do something like that? It solves nothing and complicates everything.
Quote:Original post by 3dcgmodeling
for example. when I write ltstr() ,what happen? first I guess ltstr constructor called ,second operator()(function) called. do I guess right?
Note that ltstr is a typename like int or char, not the name of an instance. ltstr() would call the contructor creating an unnamed temporary. If you write
ltstr foo;
then you can write foo(arg1,arg2) to call operator() on that instance.
To answer your second question Foot Soldier, you could create function objects like that and give them to a sorting routine. In fact, std::sort can take a function object as a parameter to determine which object is "less" than another. I am sure there are many other possible uses.
To answer your question, 3dcg -- calling ltstr() wouldn't work (well, at least in the sense you want it to). You need to create an instance of it first. Here is an example:
Edit: Beaten, damnit!
To answer your question, 3dcg -- calling ltstr() wouldn't work (well, at least in the sense you want it to). You need to create an instance of it first. Here is an example:
ltstr comparator;std::cout << comparator("foo", "bar") << std::endl;
Edit: Beaten, damnit!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement