Sign in to follow this  
Timberl

pointers/references to C++ vectors as arguments

Recommended Posts

Timberl    106
was drifting through previous messages on this board and found one that has concerned me:
Quote:
By - jyk Posted - 10/15/2004 12:06:26 AM Another useful use is passing variables by reference. You usually don't want to pass a Vector by value because of the copy overhead. But if you pass a Vector&, the called function can mess with it, which you may not want. So you pass it as a const Vector& - low overhead, and safe. You can also declare member functions as constant, which means that the function won't change any of the member variables. The compiler can do some optimizations this way. Also, you can safely call a const function on a const object or const object&. More generally, you build a 'chain of consts' that runs through your code and keeps you from modifying things you shouldn't. It's just good practice, and makes your code safer and more robust.
I had thought that having something like: F( vector<int> v ){ ... } would not make a local-copy of the entire argument-vector when F was called - I guess because I imagined vectors were pretty much like arrays - which are pretty much pointers. Do I take it from this that you actually have to specify when you want to pass a pointer to a vector rather than the vectors contents?? (!!) and then, why has 'jyk' written vector& and not vector* - by my limited understanding & and * are opposite functions - though I'm suspecting this is a use of & I don't know about? a last and less-pressing point, originally I through const on a function effectively made it static(as per java). Then through trial and error I decided that actually the keyword static made a function static in C++ and didnt understand what const was for. Now reading this post I get the impression that actually const pretty much does make something 'static' in the java sense of the word so am confused as to what if any difference there is between them and when you would use each.

Share this post


Link to post
Share on other sites
ToohrVyk    1596
& is used in a function argument list to denote which parameters are passed by reference.

function( int x ); // x passed by value
function( int & x ); // x passed by reference

Passing a vector without passing it by reference creates a copy of that vector, which means it will copy all the contents of the vector as well. One advantage of a vector (or other containers) over an array is that when you pass it by value, the original cannot be modified (which is actually what everyone expects from passing an object by value).

Share this post


Link to post
Share on other sites
ToohrVyk    1596
const/static are not the same thing.

static is used to indicate that a function or variable is shared across all the instances of a class (when used in a class), or that a variable keeps the same value from one call of the function to the other (when used inside a function).

const is used to indicate that the variable is read-only (when used on a variable) or that the member function can be called even on a read-only instance (when used on a member function).

Examples:


class C {
public:
void function( void );
void const_function( void ) const;
};
void call( const C & instance ) {
instance.function( ); //error: function() is not const
instance.const_funcion( ); //works fine
C nonconst;
nonconst.function( ); //works
nonconst.const_function( ); //works
}



You can also create an overloaded "const" version of any member function.

const is used to detect errors in code by marking items as read-only, and throwing a compiler error if a bit of code might modify something that shouldn't be modified.

Share this post


Link to post
Share on other sites
Oluseyi    2112
Quote:
Original post by Timberl
...I guess because I imagined vectors were pretty much like arrays - which are pretty much pointers.
std::vector is a class, a type. Arrays are not a type per se. They're very, very different.

Quote:
and then, why has 'jyk' written vector& and not vector* - by my limited understanding & and * are opposite functions - though I'm suspecting this is a use of & I don't know about?
The address-of operator (&) is one use of the symbol. The other, which you don't know about, is used strictly in initializations of references.
int i;
int &j = i; // j is a reference to i
i = 5;
cout << j << endl; // prints 5
j = 9;
cout << i << endl; // prints 9

When used in the argument list of a function/method, it causes the argument to be passed by reference rather than by value. This means that the original object, not a copy, is made available to the function/method, and can be altered by it. If alteration is not desirable, then a const reference is required:
int function(const int &n);

One interesting thing about a const reference is that it allows for the passing of literals where it would not otherwise be possible (you can't have a non-const reference to a literal because literals are implicitly const). This is frequently used when manipulating instances of std::string:
int modifyString(const std::string &str);

...

modifyString("Let\'s Use a Literal!");


Quote:
a last and less-pressing point, originally I through const on a function effectively made it static(as per java).
const in C++ has two interpretations. One declares a constant (non-mutable) variable. The other marks a method as not modifying the object it is called on, making it valid for use with a const instance or reference. The former use, when in conjunction with pointers, can refer to the pointer itself or the value pointed to. const pointer, pointer to const and const pointer to const.

Gotta love it.

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by Timberl
what does

F( int& x )

give you that

F( int* x )

doesn't?

(I'm always amazed by the response times here btw - good work)


the first x is a reference to an int and it gives you true pass-by-reference the second x is variable of type pointer to int and can be used to simulate pass-by-reference but its not its always pass-by-value but only a copy of address is made, there for its completely valid to have a reference to a pointer.

For trivial use cases of reference types the compiler can optimize such that the passed instance is the instance no such copy (copy of address that is) is made but for pointers a copy is always made but its just a copy of address.

The standard states its always safe to assume a reference to an instance is the instance.

A more interesting case is:


void foo(const int& x);

vs

void foo(const int* const x);


constant references can be used with literals and temporaries which is different to how pointers work,

in general always perfer using reference types over pointers in c++ in the wright places of course.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this