BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie: calling a function with "var&" parameter

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
jonjon
Guest





PostPosted: Fri Dec 30, 2005 5:55 pm    Post subject: Newbie: calling a function with "var&" parameter Reply with quote



Hi,
I've a piece of code which is correctly compiling in MSVC but generates
errors in BDS2006, and I think this is because of my lack of knowledge in
the language. I hope somebody will be able to help me.

- "vector3" is a class
- I have a class aabb whose constructor is "aabb(vector3& a_Pos, vector3&
a_Size) {};"
- Somewhere in my code I call this constructor like this "return aabb(
vector3(0,0,0), vector3(1,1,1));"

Now BDS complains on that call that: E2285 Could not find a match for
'aabb::aabb(vector3,vector3)'

I've succeeded by first creating the vectors like this "vector3
v1(0,0,0)..." and then calling the constructor with those parameters but
isn't it a shorter way of doing this ? Why does MSVC likes it but BCC
doesn't ?

Thanks for any help.

Regards,
John.


Back to top
Alan Bellingham
Guest





PostPosted: Fri Dec 30, 2005 6:17 pm    Post subject: Re: Newbie: calling a function with "var&" parameter Reply with quote



"jonjon" <none (AT) for (DOT) now> wrote:

Quote:
I've a piece of code which is correctly compiling in MSVC but generates
errors in BDS2006, and I think this is because of my lack of knowledge in
the language. I hope somebody will be able to help me.

*Incorrectly* compiling in VC. *Correctly* failing in BCB.

(And still compiling in Visual C++ 2005 Express, which does surprise
me.)

Quote:
- "vector3" is a class
- I have a class aabb whose constructor is "aabb(vector3& a_Pos, vector3&
a_Size) {};"
- Somewhere in my code I call this constructor like this "return aabb(
vector3(0,0,0), vector3(1,1,1));"

Now BDS complains on that call that: E2285 Could not find a match for
'aabb::aabb(vector3,vector3)'

I've succeeded by first creating the vectors like this "vector3
v1(0,0,0)..." and then calling the constructor with those parameters but
isn't it a shorter way of doing this ? Why does MSVC likes it but BCC
doesn't ?

Because BDS2006 is getting it right - it's refusing to bind a
*non-const* reference to a temporary.

The reason for this is that the C++ Standard Committee came to the
conclusion that if you pass a non-const reference to a function, then
the passed object is (presumably) to be updated. On the other hand, it
makes no sense to modify a temporary variable that is going away once
the function call is complete.

Ergo, an attempt to pass a temporary to a non-const reference was almost
guaranteed to be an error, yet a workaround for those few cases where it
was meant was simplicity itself (you found it yourself).

Question - why are you using a non-const reference? What do you expect
to happen?

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK

Back to top
jonjon
Guest





PostPosted: Fri Dec 30, 2005 8:22 pm    Post subject: Re: Newbie: calling a function with "var&" parameter Reply with quote



Quote:
Ergo, an attempt to pass a temporary to a non-const reference was almost
guaranteed to be an error, yet a workaround for those few cases where it
was meant was simplicity itself (you found it yourself).

Thank you for your clear explanation Alan. I guess what I didn't understand
was the "vector3& v1" but I understand it's the same as "vector3 & v1" or
"vector3 &v1" which means that the function is expecting a reference of
vector3... a pointer parameter I guess in the delphi world.

Quote:
Question - why are you using a non-const reference? What do you expect
to happen?

The thing is, it's not originally my code and I was only trying to
understand it and make it compile in BDS2006. I pretty much think now that
it was rapidely coded in MSVC which didn't complain (or maybe only warned ?)
about this call.

Thanks for the clarification Alan.

Regards,
John.



Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Mon Jan 02, 2006 5:31 pm    Post subject: Re: Newbie: calling a function with "var&" parameter Reply with quote

"jonjon" <none (AT) for (DOT) now> writes:

Quote:
Ergo, an attempt to pass a temporary to a non-const reference was almost
guaranteed to be an error, yet a workaround for those few cases where it
was meant was simplicity itself (you found it yourself).

Thank you for your clear explanation Alan. I guess what I didn't understand
was the "vector3& v1" but I understand it's the same as "vector3 & v1" or
"vector3 &v1" which means that the function is expecting a reference of
vector3... a pointer parameter I guess in the delphi world.

The spacing of the reference is immaterial. vector3&v1 is yet another
way to spell it.

What Alan is saying, is that the reference is to a non-const object
meaning your interface says "I plan to modify this value you are
passing in." Since an unnamed temporary cannot be bound to a
reference to non-const, the code should not compile. (The reasoning
is that modifying a temporary is usually a programming mistake, and
it's hard to track down without compiler support. If you really need
to modify the value, simply declare it with a name, then pass in the
named object to the function instead of creating one in the function
invocation itself.)

If you don't really need to modify the value, the function signature
should be a reference to a const object, like this:

void myfunc (vector3 const & v1)

Now, you can pass an unnamed temporary object to the above function,
because the reference is to a const vector3, and the interface says,
"I won't change this".

--
Chris (TeamB);

Back to top
jonjon
Guest





PostPosted: Tue Jan 03, 2006 2:19 pm    Post subject: Re: Newbie: calling a function with "var&" parameter Reply with quote

Quote:
If you don't really need to modify the value, the function signature
should be a reference to a const object, like this:

void myfunc (vector3 const & v1)

Great and very good explanation. Thanks a lot to both of you.

Regards,
John.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.