473,386 Members | 1,621 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,386 software developers and data experts.

Clarification in Thinking in C++ Vol 1

Hi, i am now reading thru the thinking in C++ and i have some doubts at
section CONST REFERENCES as
The use of const references in function arguments is especially
important because your function may receive a temporary object.
This might have been created as a return value of another function
or explicitly by the user of your function. Temporary objects are
always const, so if you don't use a const reference, that argument
won't be accepted by the compiler. As a very simple example,
page - 498 , chapter 11, Ref & Copy ctor,

can any pls make me understand this. I tried reading thru again &
again, but i am not able to understand. This was foll by example which
i could not understand

//: C11:ConstReferenceArguments.cpp
// Passing references as const
478 Thinking in C++ www.BruceEckel.com
void f(int&) {}
void g(const int&) {}
int main() {
//! f(1); // Error
g(1);
} ///:~
Pls clarify

Regards
JK

Jun 19 '06 #1
4 1590
gopal posted:

Temporary objects are always const

That's incorrect.
class ArbitraryClass {
public:

int data;

void NonConstMethod()
{
data = 44;
}

};
int main()
{
ArbitraryClass().NonConstMethod();
}
can any pls make me understand this. I tried reading thru again &
again, but i am not able to understand. This was foll by example which
i could not understand <snip> void f(int&) {}
void g(const int&) {}
int main() {
//! f(1); // Error

There's only one kind of thing which you can bind a non-const reference to,
and that's a non-const l-value.
Const references, on the other hand, are far more flexible, and you can
bind them to anything, even an R-value:
const int& i = 56;
--

Frederick Gotham
Jun 19 '06 #2
gopal wrote:
Hi, i am now reading thru the thinking in C++ and i have some doubts
"Doubts"? About what?
at section CONST REFERENCES as
The use of const references in function arguments is especially
important because your function may receive a temporary object.
A non-const reference cannot be bound to a temporary. That's a C++
language requirement.
This might have been created as a return value of another function
or explicitly by the user of your function. Temporary objects are
always const,
That is simply not true. Disallowing of binding non-const references
to temporaries has *nothing* to do with possible constness of the
temporary object. Besides, in most cases temporaries are *not* at all
constant.
so if you don't use a const reference, that argument
won't be accepted by the compiler. As a very simple example,
page - 498 , chapter 11, Ref & Copy ctor,

can any pls make me understand this. I tried reading thru again &
again, but i am not able to understand. This was foll by example which
i could not understand
What exactly *could not* you understand?

//: C11:ConstReferenceArguments.cpp
// Passing references as const
478 Thinking in C++ www.BruceEckel.com
void f(int&) {}
void g(const int&) {}
int main() {
//! f(1); // Error
g(1);
} ///:~
Pls clarify


In 'f(1)' expression the temporary behind the numeric literal 1, which
is created at some point before calling the function and is destroyed
right after full expression evaluation, cannot be bound to a reference
to non-const 'int' as calling 'f' would require. That's why the compiler
reports an error here.

The explanation is a bit more convoluted than that temporaries are const.
They are not. The reason is that during binding a temporary to a reference
another temporary can be created if needed for proper type matching. For
example:

void f(double& d);
foid g(double const& d);

f(1); // '1' is integer
g(1); // '1' is integer

Now, if 'f' wanted to try to change its argument, and a temporary of type
'double' were created, then the change would never be propagated to the
temporary associated with it, since there would be another, intervening,
temporary. That's why it was decided to only allow references to const
to bind to a temporary.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '06 #3
Victor Bazarov posted:
That's why it was decided to only allow
references to const to bind to a temporary.

You probably meant something like:

It was decided to only allow references to non-const to bind to non-const
L-values.
--

Frederick Gotham
Jun 19 '06 #4
Frederick Gotham wrote:
Victor Bazarov posted:
That's why it was decided to only allow
references to const to bind to a temporary.

You probably meant something like:

It was decided to only allow references to non-const to bind to
non-const L-values.


Whatever.
Jun 19 '06 #5

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

Similar topics

22
by: xmp333 | last post by:
Hi All, I am trying to hide my JavaScript source. The method I chose was to keep all the important source in a password protected folder, and then use a SRC="folder/script.js" to include it...
9
by: Adam | last post by:
Hi, I am having problems having an include file rendered in my browser (IE6). The include file contains <A href> tags to be used as a navigation bar in the footer of a .html file. I am...
5
by: Krishna | last post by:
I have three typedefs are defined below. typedef char* PCHAR; typedef const PCHAR PCCHAR1; typedef const char* PCCHAR2; In a piece of code, these typedef's are used as follows. PCCHAR1 pc1...
15
by: deko | last post by:
With all due respect to Stan Leszynski and Greg Reddick, I'm still wondering how to name controls on forms in my Access MDB. Correct me if I'm wrong... If a textbox or other control on a form...
14
by: Kristo | last post by:
Since there's no strtoi function in standard C, I've been searching the clc archives for the proper way to store the result of strtol to an int. My search has yielded conflicting results. About...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
3
by: mbc | last post by:
According to the official documentation: ------------------------------------------------------------------------- Limitations of PostgreSQL Maximum size for a database unlimited (4 TB...
9
by: op | last post by:
Is there any difference between these two ways of declaring a pointer? 1) int* a; // a is a pointer where an integer is stored 2) int *a; // value stored in a is an integer. Can we use...
4
by: vivekian | last post by:
Hi, Have this following hierarchy which am implementing for a networking program. The base class 'ASocket' is the base class from which 'AListener' and 'ATalker' inherit . None of the functions...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.