473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 &
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 '06 #1
5 1671
mi******@gmail.com 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...
Feb 15 '06 #2
On 15 Feb 2006 03:18:01 -0800, mi******@gmail.com 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
No**********@Home.com
Feb 15 '06 #3
Bob Hairgrove wrote:
On 15 Feb 2006 03:18:01 -0800, mi******@gmail.com 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...
Feb 15 '06 #4
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 '06 #5
Oh, I missed the char* constructor! I'm pretty sure you are right about
that, Ben.

This combined with the:
QString::operator 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 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: Giff | last post by:
Hi, I am trying to change my way of programming (I am still learning) , in particular I am putting an effort in passing const ref to functions, when possible. When possible means (to me) when...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
1
by: iammilind | last post by:
In one of my code, I was using vector<> for certain class. In one of my struct, I have 'const' member data. However, vector<>::clear() throws compile error with that:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.