Results 1 to 16 of 16

Thread: Template class

Hybrid View

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

    Default Re: Template class

    I don't think steg90 wants to make a hijacking pointer. It is my understanding he wants normal pointers, except that a delete on them also sets them to NULL.

    Of course, you can't catch a delete that happens elsewhere in the program (where other pointers point to the same object) except if the pointer class only points to special classes that will notify it. This is what QPointer does. It can only point to QObject descendants.

    So the best you can do is make the pointer NULL after you call delete on that specific pointer. Which makes the class purely a convenience class.

    You can't overload the delete operator to take a non-pointer. That is where the compiler error comes from. So a del() function will have to do.

    Here is your convenience class:

    Qt Code:
    1. template<class T>
    2. class Pointer {
    3. public:
    4.  
    5. Pointer (T* p) {
    6. _ptr = p;
    7. }
    8.  
    9. void del() {
    10. delete _ptr;
    11. _ptr = NULL;
    12. }
    13.  
    14. // More operators. Like the dereferencing operators: * and ->
    15.  
    16. private:
    17.  
    18. T* _ptr;
    19. };
    To copy to clipboard, switch view to plain text mode 

    I just read your latest post, steg90. Why would you want to give a pointer the value NULL if it falls out of scope anyway?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  2. #2
    Join Date
    May 2007
    Posts
    301
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    46
    Thanked 3 Times in 3 Posts

    Default Re: Template class

    Hi,

    So I would have to call del()? Now the user needs to remember to call that...

    I thought QPointer took a new object and when I deleted that object it sets it to NULL for me, just was trying to emulate that in my own template class, but as you can see, no success...

    Regards,
    Steve

Similar Threads

  1. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  2. Replies: 2
    Last Post: 16th March 2007, 09:04
  3. problem using template
    By mickey in forum General Programming
    Replies: 6
    Last Post: 18th November 2006, 15:57
  4. Replies: 2
    Last Post: 4th May 2006, 19:17
  5. How to propagate from one class to another
    By mahe2310 in forum Qt Programming
    Replies: 15
    Last Post: 20th March 2006, 01:27

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
  •  
Qt is a trademark of The Qt Company.