Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 29th, 2010, 04:57 PM
EliteHussar
Guest
 
Posts: n/a
Default C++ Tip : Binding a Reference to an Rvalue

Rvalues and lvalues are a fundamental concept of C++ programming. In essence, an rvalue is an expression that cannot appear on the left-hand side of an assignment expression. By contrast, an lvalue refers to an object (in its wider sense), or a chunk of memory, to which you can write a value. References can be bound to both rvalues and lvalues. However, due to the language’s restrictions regarding rvalues, you have to be aware of the restrictions on binding references to rvalues, too.
Binding a reference to an rvalue is allowed as long as the reference is bound to a const type. The rationale behind this rule is straightforward: you can’t change an rvalue, and only a reference to const ensures that the program doesn’t modify an rvalue through its reference. In the following example, the function f() takes a reference to const int:

Code:
void f(const int & i);  
int main()  
{  
 f(2); /* OK */  
}
The program passes the rvalue 2 as an argument to f(). At runtime, C++ creates a temporary object of type int with the value 2 and binds it to the reference i. The temporary and its reference exist from the moment f() is invoked until it returns; they are destroyed immediately afterwards. Note that had we declared the reference i without the const qualifier, the function f() could have modified its argument, thereby causing undefined behavior. For this reason, you may only bind references to const objects.
The same rule applies to user-defined objects. You may bind a reference to a temporary object only if it’s const:

Code:
struct A{};  
void f(const A& a);  
int main()  
{  
 f(A()); /* OK, binding a temporary A to a const reference*/  
}
Reply With Quote
  #2 (permalink)  
Old June 29th, 2010, 07:39 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummmm....shouldn't that last example be
Code:
struct A{};  
void f(const A& a);  
int main()  
{  
    f(new A()); /* OK, binding a temporary A to a const reference*/  
}
Or am I behind the times?
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
lvalue/rvalue vs. stack/heap hkyoon BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 4 January 11th, 2009 11:17 PM
Optimisation TIP: Viewstate jimibt BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 6 May 15th, 2007 09:57 AM
Tool Tip MunishBhatia ASP.NET 2.0 Professional 2 May 11th, 2007 05:39 PM
Tool tip in PropertyGrid somannacg C# 0 July 6th, 2006 03:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.