QObject has a list of children within it. Something like:
class QObject {
std::vector<QObject*> children;
QObject *parent;
};
The constructor basically saves the parent and adds the object to your list
QObjec(QObject *parent) : parent(parent) {
if (parent)
parent->children.push_back(this);
}
The magic stays in the destructor. He has to destroy all the children. That's easy. But he has to care if he is someone's child, he needs to remove himself from his list to avoid a% double.
virtual ~QObject() {
if (parent)
parent->removeChild(this);
for (unsigned i=0; i<children.size(); ++i) {
delete children[i];
}
}
The destructor must be virtual, because at the time of invoking it we want to be called the destructor of the correct class, and only the one of QObject.
To remove yourself from the parent list, something like this could be done:
QObject::removeChild(QObject *child) {
std::vector<QObject*>::iterator iter = std::remove(children.begin(), children.end(), child);
children.erase(iter, children.end());
}
I just did not really understand what you meant by builder that does not accept assignments.