I want to loop over a vector of objects each containing another vector of objects to be looped over in turn. The subvector/contained vector object members need to be able to be able to be modified and not copy constructed or implicitly shared. They are accessed from different worker methods. See the code below. Does this work? If not what's the best way to do this?
class Worker : public QObject { public: void backup(); void startScans(); void startBackups(); private: QVector<BackupJob> jobs; } class BackupJob { public: QVector<BackupJob> getSubJobs() { return subJobs; } private: QVector<BackupJob> subJobs; } void Worker::backup() { startScans(); startBackups(); } void Worker::startScans() { for (BackupJob& job : jobs) for (BackupJob& subJob : job.getSubJobs()) //do scans on each subjob here } void Worker::startBackups() { for (BackupJob& job : jobs) for (BackupJob& subJob : job.getSubJobs()) //do backups on each subjob here }
getSubJobs()is returning a copy of the vector. Perhaps that's okay with Qt's QVector, as it does have implicit sharing to ensure copy construction is fast. You may want to consider providing abackup()method forBackupJobtoo, and call that recursively, instead of expectingWorkerto deal with the details. That allowsBackupJobto stay in control of its private members without needing to make them available via accessors.