Results 1 to 7 of 7

Thread: checking for null

  1. #1
    Join Date
    Jan 2006
    Posts
    39
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default checking for null

    This may be a ridiculous question. And it may be just a C++ question. But I am totally lost here.

    I would think that if I have declared an object, say a QListView, in a header file, but have not yet initialized that object, I could test for that declared but not initialized state with

    Qt Code:
    1. if(sequenceList == NULL)
    To copy to clipboard, switch view to plain text mode 

    where sequenceList is the name of my QListView. But it is always returning false, whether initialized or not. I've also tried:

    Qt Code:
    1. if(sequenceList == null)
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. if(sequenceList == 0)
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong here? Any help would be greatly appreciated.

    Thanks,
    Derek

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: checking for null

    Quote Originally Posted by drkbkr
    This may be a ridiculous question. And it may be just a C++ question. But I am totally lost here.

    I would think that if I have declared an object, say a QListView, in a header file, but have not yet initialized that object, I could test for that declared but not initialized state with

    Qt Code:
    1. if(sequenceList == NULL)
    To copy to clipboard, switch view to plain text mode 

    where sequenceList is the name of my QListView. But it is always returning false, whether initialized or not. I've also tried:

    Qt Code:
    1. if(sequenceList == null)
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. if(sequenceList == 0)
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong here? Any help would be greatly appreciated.

    Thanks,
    Derek
    Depends on your compiler but pointers are rarely set to 0. You've got to do it in the constructor:

    Qt Code:
    1. SomeClass::SomeClass(...)
    2. : sequenceList(0)
    3. {
    4. //other stuff
    5. }
    To copy to clipboard, switch view to plain text mode 

    which is the same as :

    Qt Code:
    1. SomeClass::SomeClass(...)
    2. {
    3. sequenceList = 0;
    4. //other stuff
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by fullmetalcoder; 10th March 2006 at 22:16.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. The following user says thank you to fullmetalcoder for this useful post:

    drkbkr (10th March 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: checking for null

    Quote Originally Posted by fullmetalcoder
    Depends on your compiler but pointers are rarely set to 0.
    It is undefined to what value they will be set. They could be zero or not there is no default value.
    Quote Originally Posted by fullmetalcoder
    which is the same as :
    It's not exactly the same, better initialize to rather than assign the default values http://www.parashift.com/c++-faq-lit....html#faq-10.6

  5. #4
    Join Date
    Jan 2006
    Location
    N.B. Canada
    Posts
    47
    Thanked 8 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: checking for null

    Strictly speaking, in C++, a null pointer is 0, not 0l, 0L, null, or NULL. It's just something to be aware of, since 0 is usually the convention, and also some people feel pretty strongly about it. :/

    Bojan
    The march of progress:
    C:
    printf("%10.2f", x);
    C++:
    cout << setw(10) << setprecision(2) << showpoint << x;
    Java:
    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(2);
    formatter.setMaximumFractionDigits(2);
    String s = formatter.format(x);
    for (int i = s.length(); i < 10; i++) System.out.print(' ');
    System.out.print(s);

  6. #5
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: checking for null

    NULL is simply a macro that will be replaced with 0 by the preprocessor. I think it's more clear. Pointers can be NULL. Integers can be the numeric value 0.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  7. #6
    Join Date
    Mar 2006
    Posts
    22
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: checking for null

    Quote Originally Posted by Bojan
    Strictly speaking, in C++, a null pointer is 0, not 0l, 0L, null, or NULL. It's just something to be aware of, since 0 is usually the convention
    Depends which books you read. I always use NULL.

    And holy wars have started about less.

  8. #7
    Join Date
    Mar 2006
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: checking for null

    In c++ a pointer is only 0 if you set it to 0. You can do this in the initializer list.
    You would also need to do it after any delete.

    Qt provides a QPointer that starts "nulled", gets "nulled" after a delete
    and has a member isNull().

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. Adding spell checking to lineedits, textedits, ...
    By jiveaxe in forum Qt Programming
    Replies: 12
    Last Post: 7th October 2009, 10:04
  3. Crash while checking the index is of particular level of a tree
    By kapil sharma in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2008, 08:21
  4. StyleSheet syntax checking
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 30th June 2008, 11:09
  5. Checking if bad pointer?
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2007, 10:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.