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. std::unique_ptr<QCanBusDevice> m_canDevice in another class
Forum Updated to NodeBB v4.3 + New Features

std::unique_ptr<QCanBusDevice> m_canDevice in another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 536 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.
  • D Offline
    D Offline
    dziko147
    wrote on last edited by
    #1

    Hello ,
    I want to get the

    m_canDevice 
    

    of

    std::unique_ptr<QCanBusDevice> m_canDevice
    

    in another class .
    I don't think I need shared_ptrs as PointerOwner is the only owner.

    there is a way to do this ?
    thank you

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      This somehow contradicts the idea of a unique pointer...
      Why use a pointer as member here at all?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dziko147
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:

        This somehow contradicts the idea of a unique pointer...

        first of all i use the QCanBus example of Qt .
        Second I need only one device dynamically allocated

        JoeCFDJ 2 Replies Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @dziko147 said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:

          Second I need only one device dynamically allocated

          Again: why do you need to allocate it dynamically? And why do you think you need a unique_ptr then? Hoe you did not pass a parent to your QCanBusDevice then...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #5

            @dziko147 said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:

            std::unique_ptr<QCanBusDevice> m_canDevice

            you can return a raw pointer or a weak pointer. Be aware that you do not clean it up.
            QCanBusDevice * getDevice()
            {
            return m_canDevice.get();
            }

            As Christian pointed out, this is not a good design if a pointer is used somewhere else as well. Simply replace it with shared pointer(not a crime).

            1 Reply Last reply
            1
            • D dziko147

              @Christian-Ehrlicher said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:

              This somehow contradicts the idea of a unique pointer...

              first of all i use the QCanBus example of Qt .
              Second I need only one device dynamically allocated

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • D dziko147

                @Christian-Ehrlicher said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:

                This somehow contradicts the idea of a unique pointer...

                first of all i use the QCanBus example of Qt .
                Second I need only one device dynamically allocated

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @dziko147 said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:

                @Christian-Ehrlicher said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:

                This somehow contradicts the idea of a unique pointer...

                first of all i use the QCanBus example of Qt .
                Second I need only one device dynamically allocated

                These are not reasons for using unique pointer here. Learn more C++ basics. Your question is actually not a Qt one.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi
                  What should the "other" class use m_canDevice for ?
                  I mean MainWindow has all the slots etc so
                  could that other class simply not ask MainWindow to perform operations on its behalf instead of wanting access to its private member?

                  1 Reply Last reply
                  0

                  • Login

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