Hello everyone,
I am reading some code from other people, there are some code like
this, -
class Foo {
-
-
};
-
-
Foo& func()
-
{
-
Foo foo;
-
return foo;
-
}
-
-
int main()
-
{
-
Foo& foo = func();
-
-
return 0;
-
}
-
I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.
thanks in advance,
George 7 3960
On 12月16日, 下午1时49分, George2 <george4acade...@yahoo.comwrote:
Hello everyone,
I am reading some code from other people, there are some code like
this,
-
class Foo {
-
};
-
Foo& func()
-
{
-
Foo foo;
-
return foo;
-
}
-
int main()
-
{
-
Foo& foo = func();
-
return 0;}
-
I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.
thanks in advance,
George
I'm afraid this code can not be compiled. To return a reference of
local non-static object is not allowed.
Alf P. Steinbach wrote:
And I'm sorry this is EXTREMELY OFF-TOPIC, but perhaps someone knows how
this can be?
It seems the connection problems are protocol-dependent.
Although I can't read my mail (GMail), I can read the New York Times,
but pinging that server fails:
Is your PC infected by windows ??
--
Ian Collins.
On Dec 16, 7:11 am, "Alf P. Steinbach" <al...@start.nowrote:
* yanlinlin:
On 12鏈16鏃, 涓嬪崍1鏃49鍒, George2 <george4acade...@yahoo.comwrote:
I am reading some code from other people, there are some code like
this,
-
-
-
Foo& func()
-
{
-
Foo foo;
-
return foo;
-
}
-
-
-
-
-
-
-
int main()
-
{
-
Foo& foo = func();
-
return 0;}
-
-
-
-
I want to confirm with you that it is not good code, since
we return a reference to local object instance, right? Even
if the code has expected function currently.
I'm afraid this code can not be compiled. To return a
reference of local non-static object is not allowed.
It's allowed and it should compile.
A compiler that doesn't allow it is non-conforming.
However, using that result incurs Undefined Behavior.
I think that if the compiler can prouve that the result is used
(and it is here), then it is allowed to reject the code. (The
case here is an interesting one, in fact---I'm assuming that the
same rules hold for references as for pointers, and that even
copying an invalid reference is undefined behavior. I've not
looked it up, however, so caveat empor.)
PS: Could you, or anyone, confirm whether you see this
posting? If you don't see it, simply do nothing.
I've seen it. (Rather obviously.) I even figured out something
on-topic to add so that I wouldn't be off topic in
responding:-).
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient茅e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S茅mard, 78210 St.-Cyr-l'脡cole, France, +33 (0)1 30 23 00 34
It's allowed and it should compile.
>
A compiler that doesn't allow it is non-conforming.
However, using that result incurs Undefined Behavior.
Cheers, & hth.,
- Alf
Yes, I misunderstood that.
It can compile successfully, but with a warning. And the option "treat
warning as error" misguided me about it. :P
On Dec 16, 13:39, James Kanze <james.ka...@gmail.comwrote:
On Dec 16, 7:11 am, "Alf P. Steinbach" <al...@start.nowrote:
* yanlinlin:
On 12月16日, 下午1时49分, George2 <george4acade...@yahoo.comwrote:
>I am reading some code from other people, there are some code like
>this,
> -
-
-
>Foo& func()
-
>{
-
> Foo foo;
-
> return foo;
-
>}
-
-
-
-
-
-
-
>int main()
-
>{
-
> Foo& foo = func();
-
> return 0;}
-
>
-
-
-
-
>I want to confirm with you that it is not good code, since
>we return a reference to local object instance, right? Even
>if the code has expected function currently.
I'm afraid this code can not be compiled. To return a
reference of local non-static object is not allowed.
It's allowed and it should compile.
A compiler that doesn't allow it is non-conforming.
However, using that result incurs Undefined Behavior.
I think that if the compiler can prouve that the result is used
(and it is here), then it is allowed to reject the code. (The
case here is an interesting one, in fact---I'm assuming that the
same rules hold for references as for pointers, and that even
copying an invalid reference is undefined behavior. I've not
looked it up, however, so caveat empor.)
How can compiler prove that the result is used? How can he prove i'm
not writing a thread that's supposed to be externally terminated in
the very same case as when the function happens to return a reference-
to-local, the reference later explicitely being used but unreachable
in practice? :-)
On 16 Dec, 06:09, "Alf P. Steinbach" <al...@start.nowrote:
* George2:
Hello everyone,
I am reading some code from other people, there are some code like
this,
-
-
};
-
-
-
-
-
Foo& func()
-
{
-
Foo foo;
-
return foo;
-
}
-
-
-
-
-
int main()
-
{
-
Foo& foo = func();
-
-
-
-
-
return 0;
-
}
-
-
-
I want to confirm with you that it is not good code, since we return a
reference to local object instance, right? Even if the code has
expected function currently.
It's Undefined Behavior if the function result is ever used.
Btw., could you or anyone else please confirm that you can see this posting.
It seems that all internet connections out of Norway are down at the
moment, at least via my ISP (e.g. I can't reach Microsoft's servers, nor
Google), yet articles keep dropping in in international newsgroups.
Of course, if you don't see this posting, simply don't respond.
i can c u. uk
On Dec 17, 11:56 am, Pavel Shved <Pavel.Sh...@gmail.comwrote:
On Dec 16, 13:39, James Kanze <james.ka...@gmail.comwrote:
On Dec 16, 7:11 am, "Alf P. Steinbach" <al...@start.nowrote:
* yanlinlin:
On 12鏈16鏃, 涓嬪崍1鏃49鍒, George2 <george4acade...@yahoo.comwrote:
I am reading some code from other people, there are some code like
this, -
-
-
-
-
Foo& func()
-
{
-
Foo foo;
-
return foo;
-
}
-
-
-
-
-
-
-
-
-
int main()
-
{
-
Foo& foo = func();
-
return 0;}
-
-
-
-
-
I want to confirm with you that it is not good code, since
we return a reference to local object instance, right? Even
if the code has expected function currently.
I'm afraid this code can not be compiled. To return a
reference of local non-static object is not allowed.
It's allowed and it should compile.
A compiler that doesn't allow it is non-conforming.
However, using that result incurs Undefined Behavior.
I think that if the compiler can prouve that the result is
used (and it is here), then it is allowed to reject the
code. (The case here is an interesting one, in fact---I'm
assuming that the same rules hold for references as for
pointers, and that even copying an invalid reference is
undefined behavior. I've not looked it up, however, so
caveat empor.)
How can compiler prove that the result is used?
By flow analysis.
How can he prove i'm not writing a thread that's supposed to
be externally terminated in the very same case as when the
function happens to return a reference- to-local, the
reference later explicitely being used but unreachable in
practice? :-)
That's undefined behavior as well, as far as the standard goes,
so the compiler doesn't have to take it into account. (In fact,
of course, it's also impossible to do on most modern systems;
another process can't really look at the individual instructions
you're executing.)
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient茅e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S茅mard, 78210 St.-Cyr-l'脡cole, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kench |
last post by:
I was curious and playing with pointers and references to see what's
different between them.
Other than the obvious ones involving C++ syntax & things like references
cannot be modified with...
|
by: cppaddict |
last post by:
I'd like to know what goes on under the hood when methods return
objects. Eg, I have a simple Point class with two members _x and _y.
It's constructor, copy constructor, assignment operator and...
|
by: Gama Franco |
last post by:
Hi,
I'm designing an interface for a shared library, and I would like to
know if there is a standard about how to return an object to the user. I
will use exceptions to report errors, so there...
|
by: Neal Coombes |
last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for
better from the unmoderated groups:
-------- Original Message --------
Subject: Return appropriately by value, (smart)...
|
by: Nascimento |
last post by:
Hello,
How to I do to return a string as a result of a function.
I wrote the following function:
char prt_tralha(int num)
{
int i;
char tralha;
|
by: Jess |
last post by:
Hello,
I understand that if a function "f" has a local variable "a", and
after it returns, "a" vanishes. If "f" returns "a" as a result, then I
noticed the following:
1. if the return type is...
|
by: George2 |
last post by:
Hello everyone,
I am reading some code from other people, there are some code like this,
class Foo {
};
|
by: zheng4t |
last post by:
I am learning C++. I found the following code in the book The C++
Programming Language by Bjarne Stroustrup.
struct Pair {
string name;
double val;
};
vector<Pairpairs;
double& value(const...
|
by: Astley Le Jasper |
last post by:
Sorry for the numpty question ...
How do you find the reference name of an object?
So if i have this
bob = modulename.objectname()
how do i find that the name is 'bob'
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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: 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...
| | |