Thread: Function returning a function

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    7

    Function returning a function

    Hello,
    I have a class using a hash table. The constructor of the hash table takes as parameters the number of buckets, a pointer to a hash function, and a pointer to a comparison function. My problem is that the size of the hashtable is not necessarily constant, so I can't define a function that takes into account the size of the hash table (i.e. my hash function would just mod the key by the size of the hashtable). Is there a way around this (e.g. creating a function that returns a function)?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    As long as the function returns the same type as the returnee

    Code:
    int ReturnMe ()
    {
        
        return number;
    
    }
    
    int GetReturnMe ()
    {
    
        return ReturnMe ();
    
    }
    I believe that works.
    What is C++?

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I'm a little confused by what you mean? In C functions are not first-class, because you cannot return functions, only function pointers. But ordinarily you would store the table size along with the buckets, and you would, then, write functions using the tablesize.

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by shawn_drn
    Hello,
    I have a class using a hash table. The constructor of the hash table takes as parameters the number of buckets, a pointer to a hash function, and a pointer to a comparison function. My problem is that the size of the hashtable is not necessarily constant, so I can't define a function that takes into account the size of the hash table (i.e. my hash function would just mod the key by the size of the hashtable). Is there a way around this (e.g. creating a function that returns a function)?
    Your questions seems a bit confusing. Are you wanting to dynamically allocate memory for the size of the hash Table? Regardless of the answers this is the wrong forum for your questions.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you wanting to dynamically allocate memory for the size of the hash Table?
    That's what I was trying to figure out: Why they think they can't allocate a hash table dynamicly and return it. I'm not seeing where they're getting confused. Except for the minor point of them talking about constructors in the C forum.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> The constructor ... takes as parameters the number of buckets
    Isn't this the size of the table?

    >> problem is that the size ... is not necessarily constant, so I can't define a function that takes into account the size of the hash table
    The size of the hash table will be a member variable of the class. Since the size isn't constant, you'll have to create the table dynamically.
    You can pass around a dynamically created array by simply using a point to the array type. For example, "void foo(int *p)" can receive an int[100] or an int[1000] array.

    For future posts, use the C++ forum for C++ stuff.

    gg

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Moved to C++ forum.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Problem in returning value from the dll exported function
    By dattaforit in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2006, 04:30 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM