Purpose of QScopedPointer
-
Depending on values in a configuration file my application shall for example decide, wether to use one derivative of a QAbstractTableModel or another. If I declare both of those in a header file, one will not be used.
ModelClassDerivative_X *model_a; ModelClassDerivative_Y *model_b;As far as I understand it, one should avoid that, because the unused model (or rather pointer to it), might cause problems. One way to do it seems to be to use a QScopedPointer. Is that correct?
The documentation, that I've found so far, is rather cryptic. Anyway, first of all, I need to know if I'm on the right track.
Kind regards,
Andreas
-
Hi,
Not really no. There are several ways to do that:
- A union
- A base class pointer
The union will ensure that you are using only one object of a given type.
The base class pointer is really that, you assign it your derived class and your done.Unless you added some dedicated function to each class independently, the base class pointer will be simple to manage.
-
Depending on values in a configuration file my application shall for example decide, wether to use one derivative of a QAbstractTableModel or another. If I declare both of those in a header file, one will not be used.
ModelClassDerivative_X *model_a; ModelClassDerivative_Y *model_b;As far as I understand it, one should avoid that, because the unused model (or rather pointer to it), might cause problems. One way to do it seems to be to use a QScopedPointer. Is that correct?
The documentation, that I've found so far, is rather cryptic. Anyway, first of all, I need to know if I'm on the right track.
Kind regards,
Andreas
@andi456
An unused pointer to something, in itself and the way that you show, doesn't have consequences. Wouldn't do any harm to explicitly iniliatlize it tonullptr. There are nicer ways to do what you want.QScopedPointercan save you from having todeleteit when it goes out of scope. But that is a separate issue from having two pointers, only one of which you are using. -
A andi456 has marked this topic as solved on