473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it safe to refer to function-returned values?

Like this:

Type& a=b();

I wonder if the value b returned can be refered this way.

Nov 8 '06 #1
4 1271
* Magcialking:
Like this:

Type& a=b();

I wonder if the value b returned can be refered this way.
Depends on the definition of Type and the function b().

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 8 '06 #2
Magcialking:
Like this:

Type& a=b();

I wonder if the value b returned can be refered this way.

Firstly, it depends on:

(1) Whether "Type" is a const type or not, e.g. typedef int const Type;

Secondly, it depends on:

(1) Whether the function returns by value or by reference.

If the function returns by reference, then it also depends on:

(1) The lifetime of the object to which it refers.

First thing you'll notice is that you can't bind a "reference to non-
const" to a function invocation which returns by value (or any R-value for
that matter).
Second thing you'll notice is that if the function returns by reference,
then you better make sure that the referred object is still valid after the
function returns. Here's a few examples of good and bad:

int Func1() { return 5; }

int &Func2() { int i = 5; return i; }

int const &Func3() { int const i = 5; return i; }

int main()
{
int &r1 = Func1(); /* Compiler ERROR */

int const &r2 = Func1(); /* OK! */

int &r3 = Func2(); /* Object already destroyed! */

int const &r4 = Func2(); /* Object already destroyed! */

int &r5 = Func3(); /* Compiler ERROR */

int const &r6 = Func3(); /* Object already destroyed! */
}

--

Frederick Gotham
Nov 8 '06 #3
Frederick Gotham:
int const &r2 = Func1(); /* OK! */

I should have explained this better. The reason why this is OK is that C++
has special rules for binding a "reference to const" to an R-value.
Specifically, the following:

int const &r = 5;

should behave as if it were:

int const rval = 5;
int const &r = rval;

The return value from a function which returns by value is an R-value.

--

Frederick Gotham
Nov 8 '06 #4
Thanks so mush Gotham, this really helped a lot.

"Frederick Gotham дµÀ£º
"
Frederick Gotham:
int const &r2 = Func1(); /* OK! */


I should have explained this better. The reason why this is OK is that C++
has special rules for binding a "reference to const" to an R-value.
Specifically, the following:

int const &r = 5;

should behave as if it were:

int const rval = 5;
int const &r = rval;

The return value from a function which returns by value is an R-value.

--

Frederick Gotham
Nov 9 '06 #5

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

Similar topics

15
2484
by: Paul Paterson | last post by:
I am trying to find a way to mimic by-reference argument passing for immutables in Python. I need to do this because I am writing an automated VB to Python converter. Here's an example of the VB code: Sub Change(ByVal x, ByRef y) x = x+1 y = y+1 End Sub
4
2720
by: Jankis | last post by:
I have problem how can i do it in C# safe code. class ABase { SetValueFromXX(string name,source,dest) // in C++ ( string name,source, int *dest) { 1. step in source i find row with name
9
5611
by: Andy Chang | last post by:
Hi, If I have this function void DoSomething(int& index) { ::Sleep(10000); DoSomethingWith(index); } Is the variable index thread safe? Meaning that if I call this from two
3
1700
by: Jankis | last post by:
I have problem how can i do it in C# safe code. class ABase { SetValueFromXX(string name,source,dest) // in C++ ( string name,source, int *dest) { 1. step in source i find row with name
11
2236
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe." I have 2 questions: 1. I thought dynamic variables are thread-safe since threads have their own
4
1415
by: Sridhar | last post by:
Hi, I have a question regarding the Global Assembly Cache (GAC) and Source Safe. We have some common dlls which we would like to put in GAC so that they can be used in different applications. We are using Source Safe as a Version Control. Let's say I have created a data access library( one of common dll) and put in the source safe. I have created one web application which needs to access this data access dll. Can I put this data access...
2
5171
by: semedao | last post by:
Hi , someone know the reason and how to handle it? thanks
6
9259
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will = "kelly" with no error // but string s2 = "kelly".Substring(0,200) // results in // ArgumentOutOfRangeException
10
3717
by: _mario.lat | last post by:
hallo, what does it means "the function is not thread-safe"? thak you in advance, Mario.
44
7779
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it possible this function evolves from thread-unsafe to thread-safe in recent years? How could i find out? I am using the C library coming with GNU linux distribution. thanks a lot.
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8482
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.