Passing zero to a 'const reference'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mikaelhc@gmail.com

    #1

    Passing zero to a 'const reference'

    Playing a bit around with Trolltech's Qt library I noticed the
    following constructor:

    QProgressDialog ( const QString & labelText, const QString &
    cancelButtonTex t, ...)

    In the documentation they state that setting cancelButtonTex t to 0
    prevents the button from being shown (and it works as stated).

    But shouldn't references always point to a well-defined object? Is this
    valid c++?

    /Mikael.

  • Ben Pope

    #2
    Re: Passing zero to a 'const reference'

    mikaelhc@gmail. com wrote:[color=blue]
    > Playing a bit around with Trolltech's Qt library I noticed the
    > following constructor:
    >
    > QProgressDialog ( const QString & labelText, const QString &
    > cancelButtonTex t, ...)
    >
    > In the documentation they state that setting cancelButtonTex t to 0
    > prevents the button from being shown (and it works as stated).
    >
    > But shouldn't references always point to a well-defined object? Is this
    > valid c++?[/color]

    My guess is that you can do anything you like inside QString.

    Perhaps there is a constructor from int, and it does something special
    with a 0 value.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...

    Comment

    • Bob Hairgrove

      #3
      Re: Passing zero to a 'const reference'

      On 15 Feb 2006 03:18:01 -0800, mikaelhc@gmail. com wrote:
      [color=blue]
      >Playing a bit around with Trolltech's Qt library I noticed the
      >following constructor:
      >
      >QProgressDialo g ( const QString & labelText, const QString &
      >cancelButtonTe xt, ...)
      >
      >In the documentation they state that setting cancelButtonTex t to 0
      >prevents the button from being shown (and it works as stated).[/color]

      How is it possible to set something to 0 inside the function when it
      is passed as a const reference? Perhaps there is a const assignment
      function somewhere in the definition of QString?
      [color=blue]
      >But shouldn't references always point to a well-defined object? Is this
      >valid c++?
      >
      >/Mikael.[/color]

      References are a kind of alias for the object they refer to. Indeed,
      they must always "point to" (but watch out, references are not
      pointers!) a well-defined object. When you assign something to a
      reference, you are actually assigning it to the underlying object.

      --
      Bob Hairgrove
      NoSpamPlease@Ho me.com

      Comment

      • Ben Pope

        #4
        Re: Passing zero to a 'const reference'

        Bob Hairgrove wrote:[color=blue]
        > On 15 Feb 2006 03:18:01 -0800, mikaelhc@gmail. com wrote:
        >[color=green]
        >> Playing a bit around with Trolltech's Qt library I noticed the
        >> following constructor:
        >>
        >> QProgressDialog ( const QString & labelText, const QString &
        >> cancelButtonTex t, ...)
        >>
        >> In the documentation they state that setting cancelButtonTex t to 0
        >> prevents the button from being shown (and it works as stated).[/color]
        >
        > How is it possible to set something to 0 inside the function when it
        > is passed as a const reference? Perhaps there is a const assignment
        > function somewhere in the definition of QString?[/color]

        Find information, resources and relevant links for trolltech.com. This domain may be for sale.


        There is a constructor from a char*:

        QString::QStrin g ( const char * str )

        Which would be a valid candidate? So a temporary Qstring is constructed
        from 0 (resulting in a null string), and a reference to that temporary
        is accepted as the argument.

        Is that how it works?

        Ben Pope
        --
        I'm not just a number. To many, I'm known as a string...

        Comment

        • mikaelhc@gmail.com

          #5
          Re: Passing zero to a 'const reference'

          It does not seems there is a constructor from int:
          QString s = 5; // This won't compile
          QString s = 0; // This is OK

          But QString behaves very much like a pointer (though it is defined as a
          class), i.e.
          if (s) Debug("The string is defined");
          works.

          I'll look at the Qt source, and see if I can figure out what is
          happening.

          Comment

          • mikaelhc@gmail.com

            #6
            Re: Passing zero to a 'const reference'

            Oh, I missed the char* constructor! I'm pretty sure you are right about
            that, Ben.

            This combined with the:
            QString::operat or const char * () const

            would also allow for implicit conversions to a pointer type - if I
            understand it right. (Which would explain why
            if (s) Debug("The string is defined");
            works).

            Thanks!

            Comment

            Working...