Results 1 to 4 of 4

Thread: Check if a object is null and destroy objects

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Check if a object is null and destroy objects

    Hi people!

    I want to know how I can check if a variable that allocates a class is empty or not and I also want to know how to destroy it.

    this is because I have a variable that is an array of a class:

    ClassUser *user[5];

    then at some point in the code:

    user[connection_number] = new ClassUser();

    this is needed because I must handle multiple connection to a server and they can't be more than the number of sql licenses that we have.

    thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Check if a object is null and destroy objects

    You have to initialized the pointers to NULL first.
    You will be wise to use a container class istead of a naked pointer too.
    something like :
    Qt Code:
    1. QVector<ClassUser*> vecUsers;
    2.  
    3. //When intilaizing you do (probably in a for loop):
    4. vecUsers.push_back(new ClassUser());
    5.  
    6. //when cleaning up:
    7. for(int i=0; i<vecUsers.size(); i++) {//some like to use 'foreach'
    8. if(vecUsers.at(i))
    9. delete vecUsers[i];
    10. }
    11. vecUsers.clear();
    To copy to clipboard, switch view to plain text mode 

    note in the code above there is no need to initialize to NULL, since the vector is empty when there are not items in it,and any item in it is either NULL if new failed, or a valid pointer.
    This approach is good if you just need to hold the pointer array and use it.

    If this has to be a dynamic array that gets and removes items, a different approach might be needed, but that depends on your application internals.
    Last edited by high_flyer; 30th June 2010 at 14:30.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Check if a object is null and destroy objects

    There is a handy qDeleteAll() for cleaning such containters. So you don't need to write that loop where you delete vecUsers[i].
    I'm a rebel in the S.D.G.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Check if a object is null and destroy objects

    Correct, just make sure the type is a pointer when using, and in the case above it would fit good.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. how to check for NULL reference?
    By metow in forum Newbie
    Replies: 2
    Last Post: 14th December 2009, 02:23
  2. How to destroy an object right
    By Cruz in forum Newbie
    Replies: 7
    Last Post: 22nd January 2009, 19:43
  3. Object Not destroy
    By Sudhanshu Sharma in forum Qt Programming
    Replies: 3
    Last Post: 30th July 2008, 23:16
  4. Replies: 7
    Last Post: 18th July 2006, 21:33
  5. Replies: 1
    Last Post: 24th June 2006, 20:55

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.