Jump to content
  • Advertisement
Sign in to follow this  
3dcgmodeling

function object

This topic is 4076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:


ltstr comparator;
std::cout << comparator("foo", "bar") << std::endl;


Edit: Beaten, damnit!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!