Passing zero to a 'const reference'

M

mikaelhc

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.
 
B

Ben Pope

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
 
B

Bob Hairgrove

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.
 
B

Ben Pope

Bob said:
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
 
M

mikaelhc

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.
 
M

mikaelhc

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top