Thread: functions address

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    functions address

    I have this:
    Code:
    bool cmpItem (Item fst, Item snd) {
        ...
    }
    
    class Item {
        ...
    };
    
    class Character {
        ...
        list<Item> items;
        void addItem(Item item) {
            ...
            items.sort(cmpItem);
        }
    };
    which works fine. But just for organizing better my code, I would like cmpItem do be inside the Item class. But then the code "items.sort(cmpItem)" doesn't compile, since cmpItem is probably at the wrong scope. Is there a way to do this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You either need to make CmpItem a static function (which is probabky what you want if you want to pass it to something that takes a generic function pointer), or you need to use the "class-member function pointer", as described here. http://www.parashift.com/c++-faq-lit...o-members.html

    Most fo the time when you think you need a pointer-to-member-function, it's probably better to solve the problem with deriving different variants of the class, instead of using pointers. There are exceptions to this rule, but most of the time it's true.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, addItem should probably take a reference, not by-value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Elysia View Post
    Btw, addItem should probably take a reference, not by-value.
    I don't change the item, just add it to the list. But now that you mention it, it should be const.

    I knew the static keyword was useful :P (first time using it)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning address of Function. Pointers to Functions.
    By edunia11 in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 12:28 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  4. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM