Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to return myclass reference?
Qt 6.11 is out! See what's new in the release blog

How to return myclass reference?

Scheduled Pinned Locked Moved Solved C++ Gurus
18 Posts 7 Posters 10.1k Views 4 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.
  • JohanSoloJ JohanSolo

    You can't: to return a reference the returned object must exist outside the function scope. To "solve" the issue you must either change the signature to Contact GloballAll::getContactByUID() or to something like bool GloballAll::getContactByUID( ..., Contact& return )

    K Offline
    K Offline
    Konstantin Tokarev
    wrote on last edited by
    #9
    This post is deleted!
    1 Reply Last reply
    0
    • ? A Former User

      @VRonin said in How to return myclass reference?:

      Contact* GlobalAll::getContactByUID(const int &UID) {

      Actually I don't see why that is any better than returning nothing at all, void GlobalAll::getContactByUID(const int &UID) { ...

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #10

      @Wieland I think this is a search function so it must return a "reference" to the object with contact.UID==UID or null if it can't find the item.

      P.S.
      If you find yourself using this function a lot, consider changing allContacts into a QHash<int,Contact> and use the UID as key

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Taz742T 1 Reply Last reply
      2
      • VRoninV VRonin

        @Wieland I think this is a search function so it must return a "reference" to the object with contact.UID==UID or null if it can't find the item.

        P.S.
        If you find yourself using this function a lot, consider changing allContacts into a QHash<int,Contact> and use the UID as key

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by Taz742
        #11

        @VRonin said in How to return myclass reference?:

        or null if it can't find the item.

        No. Contact will be in my QList.

        @VRonin said in How to return myclass reference?:

        If you find yourself using this function a lot, consider changing allContacts into a QHash<int,Contact> and use the UID as key

        I do not know about QHash <T, T>, It's faster than a binary search?
        If my allContacts.size() == 10 000, 15 Checks i need maximum.

        Do what you want.

        jsulmJ 1 Reply Last reply
        0
        • Taz742T Taz742

          @VRonin said in How to return myclass reference?:

          or null if it can't find the item.

          No. Contact will be in my QList.

          @VRonin said in How to return myclass reference?:

          If you find yourself using this function a lot, consider changing allContacts into a QHash<int,Contact> and use the UID as key

          I do not know about QHash <T, T>, It's faster than a binary search?
          If my allContacts.size() == 10 000, 15 Checks i need maximum.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #12

          @Taz742
          http://doc.qt.io/qt-5/containers.html#algorithmic-complexity

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Taz742T 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Taz742
            http://doc.qt.io/qt-5/containers.html#algorithmic-complexity

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by Taz742
            #13

            @jsulm @VRonin Thank guys.
            Also there are good example http://developers-club.com/posts/170017/

            @VRonin said in How to return myclass reference?:

            If you find yourself using this function a lot, consider changing allContacts into a QHash<int,Contact> and use the UID as key

            I think you are right. Thank.
            how much seconds will be lost if I search an element in QList one milion times(if i use binary search) and how much will be reserved in QHash?

            Do what you want.

            jsulmJ kshegunovK 2 Replies Last reply
            0
            • Taz742T Taz742

              @jsulm @VRonin Thank guys.
              Also there are good example http://developers-club.com/posts/170017/

              @VRonin said in How to return myclass reference?:

              If you find yourself using this function a lot, consider changing allContacts into a QHash<int,Contact> and use the UID as key

              I think you are right. Thank.
              how much seconds will be lost if I search an element in QList one milion times(if i use binary search) and how much will be reserved in QHash?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #14

              @Taz742 said in How to return myclass reference?:

              how much seconds will be lost if I search an element in QList one milion times and how much will be reserved in QHash?

              Searching in a list is O(n). Statistically you will need n/2 comparisons to find what you're looking for. Hash is O(1) - so much faster. For lookup you should use QHash or QMap not QList.

              You you want to see the difference then just write a small app and measure the time...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • Taz742T Taz742

                @jsulm @VRonin Thank guys.
                Also there are good example http://developers-club.com/posts/170017/

                @VRonin said in How to return myclass reference?:

                If you find yourself using this function a lot, consider changing allContacts into a QHash<int,Contact> and use the UID as key

                I think you are right. Thank.
                how much seconds will be lost if I search an element in QList one milion times(if i use binary search) and how much will be reserved in QHash?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #15

                @Taz742 said in How to return myclass reference?:

                how much seconds will be lost if I search an element in QList one milion times(if i use binary search) and how much will be reserved in QHash?

                Algorithmic complexity isn't measured in seconds, but with the so called "big O notation" (asymptotic complexity). Provided you can do a binary search, which implies the list is kept at all times ordered, then the complexity of the search is O(logN). For a hash it's O(1) with worst-case O(N). Now, there are nuances, but most of the time a hash would fare better than a red-black tree (QMap), which would be better than keeping an ordered list and doing a binary search on it. Of course the best is to have everything in a vector and just get the elements by index, but that's not always possible.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                4
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by A Former User
                  #16
                  • Simple code is better than complex code.
                  • Only sacrifice simplicity if you have proof that your code is too slow.
                  • The only way to prove that your code is too slow is by measuring execution time against real world data on the target machine.
                  • Don't just assume that algorithmic complexity dominates execution time; you have to measure it.

                  Just my 2 cents.

                  1 Reply Last reply
                  6
                  • K Offline
                    K Offline
                    Konstantin Tokarev
                    wrote on last edited by
                    #17
                    • Avoid premature pessimization - by default use data structures and algorithms which are right for the job, without sacrificing simplicity.

                    Otherwise you may get a death of thousand cuts for performance of your application, when it's not enough to fix a few bottlenecks because all code if uniformly slow.

                    Taz742T 1 Reply Last reply
                    4
                    • K Konstantin Tokarev
                      • Avoid premature pessimization - by default use data structures and algorithms which are right for the job, without sacrificing simplicity.

                      Otherwise you may get a death of thousand cuts for performance of your application, when it's not enough to fix a few bottlenecks because all code if uniformly slow.

                      Taz742T Offline
                      Taz742T Offline
                      Taz742
                      wrote on last edited by
                      #18

                      Pleasant discussion.

                      Do what you want.

                      1 Reply Last reply
                      1

                      • Login

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