473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing by reference... is it safe in this case?

Hi, quick question:

I have a function which takes a reference to an object as an argument.

void foo( vect3 & v );

This works fine:
vect3 v1(0.0, 0.0, 0.0);
foo(v1);

This also works fine, in VC++ .NET:
foo(vect3(0.0, 0.0, 0.0))

g++ has a fit with the second one though. I can see why it might, since the vect3() constructed object won't last very long. But my question is, does the C++ standard state somewhere that the object will last long enough for the function to evaluate it (by referencing it!) ?
Thanks!

P.S. I've posted here a lot in the past, and as far as I can remember, every post, someone has pointed out that's my post belongs in another group. I think this question is finally on-topic! ;-)

Jan 26 '06 #1
9 1692
dontspam@_dylan_.gov wrote:
Hi, quick question:

I have a function which takes a reference to an object as an argument.

void foo( vect3 & v );

This works fine:
vect3 v1(0.0, 0.0, 0.0);
foo(v1);

This also works fine, in VC++ .NET:
foo(vect3(0.0, 0.0, 0.0))

g++ has a fit with the second one though. I can see why it might, since the vect3() constructed object won't last very long. But my question is, does the C++ standard state somewhere that the object will last long enough for the function to evaluate it (by referencing it!) ?


g++ is correct in identifying this as an error. Had you declared foo
instead as:

void foo (vect3& const v);

then this would be permissible.

Likewise you could pass by value rather than by reference:

void foo (vect3 v);
Thanks!

P.S. I've posted here a lot in the past, and as far as I can remember, every post, someone has pointed out that's my post belongs in another group. I think this question is finally on-topic! ;-)

Jan 26 '06 #2
Mark P wrote:
dontspam@_dylan_.gov wrote:
Hi, quick question:

I have a function which takes a reference to an object as an argument.

void foo( vect3 & v );

This works fine:
vect3 v1(0.0, 0.0, 0.0);
foo(v1);

This also works fine, in VC++ .NET:
foo(vect3(0.0, 0.0, 0.0))

g++ has a fit with the second one though. I can see why it might, since the vect3() constructed object won't last very long. But my question is, does the C++ standard state somewhere that the object will last long enough for the function to evaluate it (by referencing it!) ?


g++ is correct in identifying this as an error. Had you declared foo
instead as:

void foo (vect3& const v);

then this would be permissible.

Likewise you could pass by value rather than by reference:

void foo (vect3 v);
Thanks!

P.S. I've posted here a lot in the past, and as far as I can remember, every post, someone has pointed out that's my post belongs in another group. I think this question is finally on-topic! ;-)


You should not pass temporary objects by reference unless the reference
is a const. Because by the time the code gets to look at the non const
temporary object that was referenced, the temporary object may or may
not exists. This will wipe out your hard drive or cause you to burst
into flames. Demons may also fly out of your nose.

Jan 26 '06 #3
Shark wrote:

You should not pass temporary objects by reference unless the reference
is a const. Because by the time the code gets to look at the non const
temporary object that was referenced, the temporary object may or may
not exists.


The temporary object lasts until the end of the full statement in which
it was created. There is no problem with the lifetime of temporary
objects passed to functions. The issue is that making changes to a
temporary object might not make sense, since it's going to go away very
shortly.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 26 '06 #4
dontspam@_dylan_.gov wrote:
Hi, quick question:

I have a function which takes a reference to an object as an argument.

void foo( vect3 & v );

This works fine:
vect3 v1(0.0, 0.0, 0.0);
foo(v1);

This also works fine, in VC++ .NET:
foo(vect3(0.0, 0.0, 0.0))

g++ has a fit with the second one though. I can see why it might, since
the vect3() constructed object won't last very long. But my question is,
does the C++ standard state somewhere that the object will last long
enough for the function to evaluate it (by referencing it!) ?
Yes, it will. However, the reason why g++ complains is that the C++ standard
says that you can't bind a temporary to a non-const reference.
P.S. I've posted here a lot in the past, and as far as I can remember,
every post, someone has pointed out that's my post belongs in another
group. I think this question is finally on-topic! ;-)


Yes, it is ;-)

Jan 26 '06 #5
Mark P <fa******@removefall2005.capsfastmail.fm> wrote:
dontspam@_dylan_.gov wrote:
Hi, quick question:

I have a function which takes a reference to an object as an argument.

void foo( vect3 & v );

This works fine:
vect3 v1(0.0, 0.0, 0.0);
foo(v1);

This also works fine, in VC++ .NET:
foo(vect3(0.0, 0.0, 0.0))

g++ has a fit with the second one though. I can see why it might, since the vect3() constructed object won't last very long. But my question is, does the C++ standard state somewhere that the object will last long enough for the function to evaluate it (by referencing it!) ?


g++ is correct in identifying this as an error. Had you declared foo
instead as:

void foo (vect3& const v);

then this would be permissible.


Actually, this should be

void foo(vect3 const& v);

What you wrote says that v is a const reference to a vect3, which is
nonsense since all references are const. Mine says that v is a
reference to a const vect3.

http://www.parashift.com/c++-faq-lit....html#faq-18.7

--
Marcus Kwok
Jan 26 '06 #6
On Thu, 26 Jan 2006 14:46:03 +0000 (UTC), ri******@gehennom.net
(Marcus Kwok) wrote:
What you wrote says that v is a const reference to a vect3, which is
nonsense since all references are const. Mine says that v is a
reference to a const vect3.

http://www.parashift.com/c++-faq-lit....html#faq-18.7

<quote>
[18.7] Does "Fred& const x" make any sense?
No, it is nonsense.
</quote>

Thank you. I'm sitting here reading about non-const references in this
thread and I was befuddled. "Did they change the way references work?"
I thought. I didn't want to look "stoopid" so I didn't respond. I was
going to go look it up and you did that. Thanks again. :-)

"If you go flying back through time, and you see somebody else flying
forward into the future, it's probably best to avoid eye contact."
- Jack Handey
Jan 26 '06 #7
Marcus Kwok wrote:
Mark P <fa******@removefall2005.capsfastmail.fm> wrote:
dontspam@_dylan_.gov wrote:
Hi, quick question:

I have a function which takes a reference to an object as an argument.

void foo( vect3 & v );

This works fine:
vect3 v1(0.0, 0.0, 0.0);
foo(v1);

This also works fine, in VC++ .NET:
foo(vect3(0.0, 0.0, 0.0))

g++ has a fit with the second one though. I can see why it might, since the vect3() constructed object won't last very long. But my question is, does the C++ standard state somewhere that the object will last long enough for the function to evaluate it (by referencing it!) ?


g++ is correct in identifying this as an error. Had you declared foo
instead as:

void foo (vect3& const v);

then this would be permissible.

Actually, this should be

void foo(vect3 const& v);

What you wrote says that v is a const reference to a vect3, which is
nonsense since all references are const. Mine says that v is a
reference to a const vect3.

http://www.parashift.com/c++-faq-lit....html#faq-18.7


Ah, yes, of course and thank you. I ordinarily write this as (const
vect3& v) but my brain appears to have flickered off last night.
Jan 26 '06 #8

"Rolf Magnus" <ra******@t-online.de> skrev i meddelandet
news:dr*************@news.t-online.com...
dontspam@_dylan_.gov wrote:
Hi, quick question:

I have a function which takes a reference to an object as an
argument.

void foo( vect3 & v );

This works fine:
vect3 v1(0.0, 0.0, 0.0);
foo(v1);

This also works fine, in VC++ .NET:
foo(vect3(0.0, 0.0, 0.0))

g++ has a fit with the second one though. I can see why it might,
since
the vect3() constructed object won't last very long. But my
question is,
does the C++ standard state somewhere that the object will last
long
enough for the function to evaluate it (by referencing it!) ?


Yes, it will. However, the reason why g++ complains is that the C++
standard
says that you can't bind a temporary to a non-const reference.


And so will the other compiler, if you set the switches correctly (/Za
in particular).
Bo Persson
Jan 26 '06 #9
On Thu, 26 Jan 2006 08:12:14 -0500 in comp.lang.c++, Pete Becker
<pe********@acm.org> wrote,
The temporary object lasts until the end of the full statement in which
it was created. There is no problem with the lifetime of temporary
objects passed to functions. The issue is that making changes to a
temporary object might not make sense, since it's going to go away very
shortly.


So let us remove from C++ everything else that can be used in a way
that "might not make sense".

Jan 27 '06 #10

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

Similar topics

3
by: Sören | last post by:
Hi, I'd like advise on passing ownership of an iostream. The idea is that my factory class/function should open a file, read enough to detect file type (eg which soundfile format), then...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
6
by: Catherine Jones | last post by:
Hi all, we need urgent help in a matter. We are trying to pass a COM object from the client to server and are facing some problems in the same. We've our client in C# as well as the Server...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
6
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
1
by: Dave Parry | last post by:
Hi, I would like to hear peoples thoughts on passing a value type by reference and then trying to store this ref in a member so it can be updated elsewhere, whereby I want the referenced value...
4
by: Mahmoud Al-Qudsi | last post by:
Hi all, I'm working on a C# project, and at some point I'm needing to pass the same stack to multiple functions. I found out the hard way that when I poss a stack by value it doesn't actually...
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
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
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,...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.