Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Destructor called in foreach loop iteration in QList derived class
Forum Updated to NodeBB v4.3 + New Features

Destructor called in foreach loop iteration in QList derived class

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 363 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NickM
    wrote on last edited by
    #1

    Hi All,
    I'm doing a fairly basic QT excercise/tutorial the involves the following class:

    class BookingList: public QList<Booking*>
    {
    public:
        BookingList();
        ~BookingList();
        int roomsAvailable(QDate d);
        bool vacancy(QDate a, QDate d);
        Booking* addBooking(Person c, QDate a, QDate d, Person* g1, Person* g2);
        void deleteAll();
    
        int NO_OF_ROOMS;
    };
    

    However something unexpected (at least to me) happens when I use a foreach loop in the roomsAvailable() function:

    BookingList::~BookingList()
    {
        qDeleteAll(*this);
        this->clear();
        qDebug()<<"destructor called...";
    }
    
    int BookingList::roomsAvailable(QDate d)
    {
       int bookedRooms = 0;
        foreach(Booking* b, *this)
        {
            if(b->booked(d))
                bookedRooms++;
        }
    
        return NO_OF_ROOMS - bookedRooms;
    }
    

    Each iteration of this loop will call the destructor directly above, which is something I really didn't expect. Is it because, in dereferencing "this" I'm sending a copy to foreach(), which subsequently gets destroyed? Is this correct? Should I avoid this because it involves a lot of copying?

    I replaced the foreach with a normal for loop and the destructor is not called (until the end of the program).

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Deriving from a container is for sure no good idea. Make it a member.

      I replaced the foreach with a normal for loop and the destructor is not called (until the end of the program).

      Take a look at the code of the macro and you will see why this happens: https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html#1044 (hint: your container will be detached/copied)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      N 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        Deriving from a container is for sure no good idea. Make it a member.

        I replaced the foreach with a normal for loop and the destructor is not called (until the end of the program).

        Take a look at the code of the macro and you will see why this happens: https://code.woboq.org/qt5/qtbase/src/corelib/global/qglobal.h.html#1044 (hint: your container will be detached/copied)

        N Offline
        N Offline
        NickM
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks for the feedback and link. I've been working from a book called "An introduction to design patterns in C++ with QT" by Ezust and Ezust. They have a handful of examples where they derive from a QList<>. I'm personally not a fan of doing it that way, but its just surprising that they do it so liberally if it's bad practice.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved