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

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 1258
* 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
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...
4
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
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
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
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...
4
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...
2
by: semedao | last post by:
Hi , someone know the reason and how to handle it? thanks
6
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 =...
10
by: _mario.lat | last post by:
hallo, what does it means "the function is not thread-safe"? thak you in advance, Mario.
44
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.