Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved "&/*parent*/" as argument?

    C++ Gurus
    5
    8
    2029
    Loading More Posts
    • 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.
    • P
      pdsc_dy last edited by

      I found the following sample coding from Qt documentation. Can you please explain the highlighted part? I am trying to understand the syntax for the argument: (const QModelIndex &/parent/). The /parent/ should be just comment, so the argument is just "const QModelIndex &"?

      int DomModel::columnCount(const QModelIndex &/*parent*/) const
      {
          return 3;
      }
      
      mrjj jsulm 2 Replies Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion @pdsc_dy last edited by mrjj

        @pdsc_dy
        its a not so good way of suppressing "unused parameter" warning.

        so yes its just
        "const QModelIndex &"

        1 Reply Last reply Reply Quote 2
        • P
          pdsc_dy last edited by

          What does the "&" stand for in this case? Thanks!

          mrjj 1 Reply Last reply Reply Quote 0
          • hskoglund
            hskoglund last edited by

            Hi, it's a reference (a fancier type of pointer, which allows you to write parent.somevar instead of parent->somevar)

            1 Reply Last reply Reply Quote 6
            • mrjj
              mrjj Lifetime Qt Champion @pdsc_dy last edited by

              @pdsc_dy
              One thing about references that makes them different from pointers.
              They cannot be null

              void somefunc1( someting &var )

              void somefunc2( someting * var)

              somefunc1(NULL) ;// will give error

              somefunc2(NULL) ;// accepted

              Chris Kawa 1 Reply Last reply Reply Quote 2
              • Chris Kawa
                Chris Kawa Moderators @mrjj last edited by

                @mrjj said:

                They cannot be null

                Sure they can: int& bar = *(int*)nullptr; - of course it's useless, dereferencing crashes the app and it generally makes no sense but there you have it - compiling just fine :)

                mrjj 1 Reply Last reply Reply Quote 3
                • mrjj
                  mrjj Lifetime Qt Champion @Chris Kawa last edited by

                  @Chris-Kawa
                  Heh. Ok. Fair point. If you really want to. :)

                  1 Reply Last reply Reply Quote 3
                  • jsulm
                    jsulm Lifetime Qt Champion @pdsc_dy last edited by

                    @pdsc_dy I moved your question to "C++ Gurus" forum as it is not related to Qt but to C++.
                    References (this & thing) are one of the C++ concepts, so you should learn C++.

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

                    1 Reply Last reply Reply Quote 3
                    • First post
                      Last post