| adn258 (63) | |||||
|
So you guys are always a great help and I appreciate that. Something I've always struggled with is pointers but I'm slowly getting better I think. So I've seen people pass reference this way
For example. Is there anything bad about doing the above code and is this how you should properly pass reference? Second I see a lot of pointers within functions that have no variables name like
What possible reason do people declare functions like this or am I missing something? Thanks guys! | |||||
|
|
|||||
| MikeyBoy (235) | |||
|
In answer to your first question, yes, that works fine, although the phrase is "pass by reference". However, C++ introduces a new way of doing it. It introduces a new construct - the reference. Instead of passing a pointer into your function, you pass a reference. To declare this, use an ampersand, & instead of an asterisk, *. Then, inside your function, you use the same semantics as the actual variable, rather than those of a pointer, like so:
The (major) advantage of using references is that you don't have to worry about pointer safety, i.e. whether or not your pointer is pointing to valid memory. The (slight) advantage of using pointers is that within the body of your function, it's immediately obvious that you're using a pointer and that any changes you make to the value of the memory it points to will be present in the calling code. With references, you can only tell whether you're using a reference or a local copy by looking at the function declaration to see if there's an & symbol.I won't go into a full tutorial on references; if you're learning C++, then you presumably already have access to reference material. Regarding your second question, I've usually seen it used in situations where your function has to conform to a particular interface, but a specific implementation doesn't actually use the argument that the interface required to be passed in. If you don't specify a name for the argument in your function implementation, then it makes it clear to anybody else reading the code that it won't be used in your function. It will also stop the compiler warning you about unused arguments. Getting rid of compiler warnings is a good thing, because it makes it easier to spot any new warnings you introduce that indicate a genuine problem with your code. | |||
|
Last edited on
|
|||
| TheIdeasMan (1757) | |
|
The other big thing about ptrs in functions, is to provide quick access to large data structures. Instead of passing the whole thing by value, just provide a pointer to it instead. As MikeyBoy said, the C++ way is to use references which is very similar. One advantage of a reference is that it always refers to something valid, whereas a ptr can be made to point at anything - valid or not. HTH | |
|
|
|
| cire (2354) | |||||||
Couple nitpicks.
Passing a pointer to an object is not passing by reference. It is passing a pointer by value.
Not true, unfortunately. References can reference things that are not valid.
| |||||||
|
|
|||||||
| TheIdeasMan (1757) | ||
|
@cire As you said - a nitpick, I thought I would post this, just for the education of anyone who was wondering. Wiki wrote:
Your example is interesting though, it is the return of a reference as opposed to a reference as a parameter. If the compiler is to complain about the snippet from wiki above, then shouldn't it complain about your example as well? I was trying to find something in the standard. | ||
|
|
||
| TheIdeasMan (1757) | ||
I found this:
| ||
|
|
||
| cire (2354) | |
|
Yes. In the example I gave, I would expect as a QOI issue to get a warning from the compiler. However, the reference is initialized to refer to a valid object. It just ceases being valid before it can be accessed. It isn't really that hard to invalidate a reference. There are three really common cases I've seen. The one I presented, the case where the objected referred to is moved (either by reallocation of the container it's in, or some other method), and the case where the object's lifetime simply doesn't match the reference's (e.g. it's erased from a container or deleted.) Things to keep in the back of your mind when dealing with references (or pointers.) | |
|
Last edited on
|
|