0

Need little help in constructing classes:

FBPermission::FBPermission(QString aName): QMap<QString, bool>()
{
    Name = aName;
    insert("read", false);
    insert("write", false);
    insert("rename", false);
    insert("delete", false);
}
FBPermission::  ~FBPermission(){}

Fachbereich::Fachbereich()
{
permissions= QList<FBPermission *>();

FBPermission * perm = new FBPermission("admin");

perm->insert("read", true);
perm->insert("write", true);
perm->insert("rename", true);
perm->insert("delete", true);
permissions.append(perm);

}
Fachbereich::~Fachbereich()
{

}

My first class is derived from QMap. In my second class I hold a list containing pointers of first class. Do I have to free them in destructor an if yes, where, in destructor of first or second class?

Frau Schmidt
  • 152
  • 11

3 Answers3

1

Yes, you should free them in the destructor.

Since Fachbereich class allocates the data, it should also be the one to deallocate it. In general if a class allocates something, it should be the one to release it as well.

Also be careful when you inherit from classes that you didn't write. For all the virtual mechanics to work correctly the base class should have a virtual destructor. If it doesn't you may run into the case that you delete the derived class while holding a pointer to the base class, which because it doesn't have a virtual destructor will simply destroy the base class without running the destructor for the derived class, leading to nasty memory leaks. There can be other very subtle gotcha's that you most likely don't want to deal with.

Sorin
  • 11,863
  • 22
  • 26
1

Yes you have to delete them. You might do it in the destructor of Fachbereich, such as:

while (!permissions.isEmpty())
    delete permissions.takeFirst();

You can't do it in the destructor of FBPermission, which is the element to be newed.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
1

First of all, its a terrible idea to derive from QMap. Primarily because its a value class and doesn't support inheritance. And the simplest way to know classes that don't support inheritance is when they don't define a virtual destructor.

Value classes need not to be allocated on heap, unless you have a compelling reason to do so.. Also, see: Prefer composition over inheritance? and this


to destruct the items, you can do it in the destructor

Fachbereich::~Fachbereich()
{
    for(auto& item : permissions)
        delete item;
}
Community
  • 1
  • 1
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • So is it a better idea to derive FBPermission from QVariant an have a member QMap in this class? – Frau Schmidt Jun 06 '16 at 07:40
  • No... [`QVariant`](http://doc.qt.io/qt-5/qvariant.html#dtor.QVariant) is also a value class... Of cause, it may be used for *non-virtual inheritance*. But, why are you keen to inherit from something? - Why not just make your container(s) in `FBPermission` data members and provide the necessary member functions for `FBPermission`? – WhiZTiM Jun 06 '16 at 07:47
  • My only think was, I need a dictionary with a name as only additional member. I just didn't know that inheriting from value class is wrong. My first programming language was delphi and there I learned to always inherit from TObject so I wanted to use QVariant here. Thanks for your explanation links! Your answer helped me the most. – Frau Schmidt Jun 06 '16 at 08:27
  • Now it crossed my mind, why to derive from `QVariant`: It is a plugin for QGIS and I want to tag the objects of this class at a `QTreeWidgetItem`, this is only possible for a`QVariant`- subtyped class. – Frau Schmidt Jun 09 '16 at 07:40