Passing zero to a 'const reference'

Discussion in 'C++' started by mikaelhc@gmail.com, Feb 15, 2006.

  1. Guest

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

    QProgressDialog ( const QString & labelText, const QString &
    cancelButtonText, ...)

    In the documentation they state that setting cancelButtonText 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.
     
    , Feb 15, 2006
    #1
    1. Advertising

  2. Ben Pope Guest

    wrote:
    > Playing a bit around with Trolltech's Qt library I noticed the
    > following constructor:
    >
    > QProgressDialog ( const QString & labelText, const QString &
    > cancelButtonText, ...)
    >
    > In the documentation they state that setting cancelButtonText 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++?


    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...
     
    Ben Pope, Feb 15, 2006
    #2
    1. Advertising

  3. On 15 Feb 2006 03:18:01 -0800, wrote:

    >Playing a bit around with Trolltech's Qt library I noticed the
    >following constructor:
    >
    >QProgressDialog ( const QString & labelText, const QString &
    >cancelButtonText, ...)
    >
    >In the documentation they state that setting cancelButtonText to 0
    >prevents the button from being shown (and it works as stated).


    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?

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


    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
     
    Bob Hairgrove, Feb 15, 2006
    #3
  4. Ben Pope Guest

    Bob Hairgrove wrote:
    > On 15 Feb 2006 03:18:01 -0800, wrote:
    >
    >> Playing a bit around with Trolltech's Qt library I noticed the
    >> following constructor:
    >>
    >> QProgressDialog ( const QString & labelText, const QString &
    >> cancelButtonText, ...)
    >>
    >> In the documentation they state that setting cancelButtonText to 0
    >> prevents the button from being shown (and it works as stated).

    >
    > 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?


    http://doc.trolltech.com/3.3/qstring.html

    There is a constructor from a char*:

    QString::QString ( 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...
     
    Ben Pope, Feb 15, 2006
    #4
  5. Guest

    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.
     
    , Feb 15, 2006
    #5
  6. Guest

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

    This combined with the:
    QString::eek:perator 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!
     
    , Feb 15, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zhiqiang Ye
    Replies:
    53
    Views:
    10,417
    Dan Pop
    Jun 28, 2004
  2. Gerard Flanagan
    Replies:
    3
    Views:
    497
    Terry Hancock
    Nov 19, 2005
  3. Christopher Benson-Manica

    Doubles and zero/negative zero

    Christopher Benson-Manica, Jun 30, 2004, in forum: C Programming
    Replies:
    4
    Views:
    716
    Walter
    Jul 1, 2004
  4. Javier
    Replies:
    2
    Views:
    626
    James Kanze
    Sep 4, 2007
  5. George2
    Replies:
    10
    Views:
    640
    Pete Becker
    Dec 17, 2007
Loading...

Share This Page