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. Purpose of QScopedPointer

Purpose of QScopedPointer

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 609 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.
  • A Offline
    A Offline
    andi456
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • A andi456

        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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @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 to nullptr. There are nicer ways to do what you want.

        QScopedPointer can save you from having to delete it when it goes out of scope. But that is a separate issue from having two pointers, only one of which you are using.

        1 Reply Last reply
        1
        • A Offline
          A Offline
          andi456
          wrote on last edited by
          #4

          Ok, thank you for your answers. For the time being I will concentrate on making the project work and then come back to optimizations like this.

          1 Reply Last reply
          0
          • A andi456 has marked this topic as solved on

          • Login

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