creating a new instance of a function?

This is a discussion on creating a new instance of a function? within the C++ Programming forums, part of the General Programming Boards category; Originally posted by DeepBlackMagic its required for an index. Try using functors (function objects) instead. STL has great support for ...

  1. #16
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by DeepBlackMagic
    its required for an index.
    Try using functors (function objects) instead. STL has great support for them, and you could use unique addresses from instances as your index. Additionally, you could make indices like this:

    Code:
    #include <functional>
    
    struct my_func : public std::binary_function<std::string, int, void>
    {
      void operator()(std::string str, int x)
      {
        // ...
      }
    };
    
    void foo()
    {
      std::unary_function<std::string, void> func1 = std::bind2nd(my_func(), 1);
      std::unary_function<std::string, void> func2 = std::bind2nd(my_func(), 2);
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Try using functors (function objects) instead. STL has great support for them, and you could use unique addresses from instances as your index. Additionally, you could make indices like this:
    True, but in this case that is not needed. My advice is to try and use the language the way it was designed to be used. Any questions - check my sig.

  3. #18
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Bubba
    True, but in this case that is not needed. My advice is to try and use the language the way it was designed to be used. Any questions - check my sig.
    This is one way it was designed to be used. They actually simplify things. Since you can have multiple instances, creating a unique index for each one is nice and simple.

    Here is perhaps a simpler example:

    Code:
    struct func
    {
      func(int x) : _index(x) {}
      void operator( ) (std::string str)
      {
      // ....
      }
    private:
      int _index;
    };
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21