Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. "&/*parent*/" as argument?

"&/*parent*/" as argument?

Scheduled Pinned Locked Moved Solved C++ Gurus
8 Posts 5 Posters 2.7k Views
  • 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 Offline
    P Offline
    pdsc_dy
    wrote on last edited by
    #1

    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;
    }
    
    mrjjM jsulmJ 2 Replies Last reply
    0
    • P pdsc_dy

      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;
      }
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

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

      so yes its just
      "const QModelIndex &"

      1 Reply Last reply
      2
      • P Offline
        P Offline
        pdsc_dy
        wrote on last edited by
        #3

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

        mrjjM 1 Reply Last reply
        0
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          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
          6
          • P pdsc_dy

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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 KawaC 1 Reply Last reply
            2
            • mrjjM mrjj

              @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 KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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 :)

              mrjjM 1 Reply Last reply
              3
              • Chris KawaC Chris Kawa

                @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 :)

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                1 Reply Last reply
                3
                • P pdsc_dy

                  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;
                  }
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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
                  3

                  • Login

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