Thread: NULL of no type~

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    NULL of no type~

    howdy~

    i used to consider NULL as general var for any type but nowdays found some problems occured when i did this in my class implementation, ie a method called "find" which purpose is to search the list specifed for certain value and return the position found or NULL otherwise. but when i return NULL my compiler report an error which said there was a type mismatch, which meant we cant return NULL as another type. so what can we do when we want NULL can be returned and satisfy any type ?

    note: finde method return a template type so it will only be defiend when run-time comes.
    Never end on learning~

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you should only return NULL if you're returning a pointer. If not, returning NULL doesnt make sense.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Some ideas:
    • Change the return type to a pointer so that you can return null.
    • Add a parameter to the function in which the found item will be placed, and return a bool indicating whether the find was successful.
    • Throw an exception if a failed search is an exceptional situation.
    • Identify a value for the type that acts as a not found (e.g. -1 or 0 for integers). This isn't so easy if you return a template type. This can also be done like the STL does it - the user can check the returned position against the position one past the end of the list.
    I like the second or fourth idea best.

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by jlou
    Some ideas:
    • Change the return type to a pointer so that you can return null.
    • Add a parameter to the function in which the found item will be placed, and return a bool indicating whether the find was successful.
    • Throw an exception if a failed search is an exceptional situation.
    • Identify a value for the type that acts as a not found (e.g. -1 or 0 for integers). This isn't so easy if you return a template type. This can also be done like the STL does it - the user can check the returned position against the position one past the end of the list.
    I like the second or fourth idea best.
    thanx man i think second one makes sense. but it seems there is still conflicts if we use another parameter to store the found item, how to declare its data type when implements it ? because it would be of a template type(if found) or NULL(not found).
    Never end on learning~

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    If you take in a reference as the position output parameter, then instead of setting it to NULL when the search fails you just leave the value unchanged. It is up to the user of your list to check the return value of the function for true or false before using the position.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM