-
Does copy occur if variable not defined as a reference?
I'm sure this has been asked before in this forum, but what I need is confirmation on this: If a variable that is assigned a reference to some struct is defined without the &, i.e.
Some_Type_t some_type = some_object.getSomeType();
instead of
Some_Type_t& some_type = some_object.getSomeType();
where method getSomeType() returns a reference to Some_Type_t, will some_type be a reference to the the Some_Type_t object or will it be a copy of the object?
I've looked in many books and places online but can't seem to find the answer. Thanks.
-
 Originally Posted by seeLTrun
I'm sure this has been asked before in this forum, but what I need is confirmation on this: If a variable that is assigned a reference to some struct is defined without the &, i.e.
Some_Type_t some_type = some_object.getSomeType();
instead of
Some_Type_t& some_type = some_object.getSomeType();
where method getSomeType() returns a reference to Some_Type_t, will some_type be a reference to the the Some_Type_t object or will it be a copy of the object?
It will be a copy, not a reference. This means that any change that you apply to some_type after its initialization will not affect any other object.
If however some_type was declared as a reference variable:
Some_Type_t& some_type = some_object.getSomeType();
some_type would indeed be an alias of another object, so chaging it would affect the actual object to which it is bound.
C++ is rather explicit (unlike Java for instance) in this respect: when you want a reference or a pointer, you must use & and * , respectively in the declaration of a variable. Otherwise, the result is an independent object.
Danny Kalev
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks