Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing zero to a 'const reference'

Reply
Thread Tools

Passing zero to a 'const reference'

 
 
mikaelhc@gmail.com
Guest
Posts: n/a
 
      02-15-2006
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.

 
Reply With Quote
 
 
 
 
Ben Pope
Guest
Posts: n/a
 
      02-15-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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...
 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      02-15-2006
On 15 Feb 2006 03:18:01 -0800, (E-Mail Removed) 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
(E-Mail Removed)
 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      02-15-2006
Bob Hairgrove wrote:
> On 15 Feb 2006 03:18:01 -0800, (E-Mail Removed) 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...
 
Reply With Quote
 
mikaelhc@gmail.com
Guest
Posts: n/a
 
      02-15-2006
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.

 
Reply With Quote
 
mikaelhc@gmail.com
Guest
Posts: n/a
 
      02-15-2006
Oh, I missed the char* constructor! I'm pretty sure you are right about
that, Ben.

This combined with the:
QString: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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing copy of a pointer to a variable vs passing the copy pereges C Programming 7 06-01-2008 02:36 PM
Can you set a class instance's attributes to zero by setting the instance to zero? Gerard Flanagan Python 3 11-19-2005 06:58 PM
Passing by const & and returning a temp vs passing by value and returningit Victor Bazarov C++ 25 03-23-2005 04:24 PM
Doubles and zero/negative zero Christopher Benson-Manica C Programming 4 07-01-2004 05:44 PM
memset all bits to zero will let float/double to zero? Zhiqiang Ye C Programming 53 06-28-2004 01:23 PM



Advertisments