473,396 Members | 2,052 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,396 software developers and data experts.

how to understand references to variables and references to constants are distinguished?

in the book the C++ program language , 5.5 :
references to variables and references to constants are distinguished
because the introduction of a temporary in the case of the variable is
highly error-pron;
an assignment to the variable would become an assignment to the - soon
to disappear - temporary. No such problem exists for references to
constants.

I don't understand it. just before the sentence, it says
const double & cdr = 1; //ok
the interprtation of it might be:
double temp = double(1);
const double & cdr = temp;

according to it, it seems to me it is reference to constant introuce
temporary not reference to variable.
Jul 22 '05 #1
1 991
ba*********@gmail.com wrote:
in the book the C++ program language , 5.5 :
references to variables and references to constants are distinguished
because the introduction of a temporary in the case of the variable is
highly error-pron;
an assignment to the variable would become an assignment to the - soon
to disappear - temporary. No such problem exists for references to
constants.

I don't understand it. just before the sentence, it says
const double & cdr = 1; //ok
the interprtation of it might be:
double temp = double(1);
const double & cdr = temp;
I think the point here is that the '1' is not of type "double". It is
actually of type "int", so to make sure the 'cdr' refers to a double,
a temporary of type "double" is made from the (int)1.
according to it, it seems to me it is reference to constant introuce
temporary not reference to variable.


Correct. However, you should have the same effect (a temporary would
be introduced) if you do

int one = 1;
const double & cdr = one;

Here, 'cdr' is not a reference to 'one'. It's a reference to some
temporary object and you can confirm that by taking an address of it
and comparing it to the address of 'one'.

Victor
Jul 22 '05 #2

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

Similar topics

23
by: Mark Parnell | last post by:
I'm relatively new to PHP, and have just converted a site from ASP to PHP. There is one thing I haven't managed to do, though. When the site was using ASP, I had one file (called variables.asp),...
1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
35
by: Sandeep Sharma | last post by:
Right from the time the first edition of K&R was released, the advantages of using symbolic constants, as opposed to "magic numbers", has been emphasized ---- and for good reason. I don't dispute...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
1
by: dnn | last post by:
How can I access dynamically loaded variables? I am trying to load an external javascript file dynamically and then access its variables. The script is loaded by the onload handler. The code...
6
by: Chris Carlen | last post by:
Hi: I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP. A module (.c file) contains external (to the funcs in that module) variables which are used to affect the operation...
4
by: amun25dringer11 | last post by:
I hear people talking about like: const float PI=3.14; that and they say you can only change it in one place but why do you need it if you can use it in a variable ex. float PI=3.14; and then...
3
by: smitty1e | last post by:
Just a fun exercise to unify some of the major input methods for a script into a single dictionary. Here is the output, given a gr.conf file in the same directory with the contents stated below: ...
3
by: mattyizzo | last post by:
Here is my code. Basically I'm trying to solve for CI and CF, where each use a bunch of constants and a variable or two which can have up to 10 values. I get the following error, however, at line...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.