&

Started by
14 comments, last by Tazel 21 years, 3 months ago
What is the difference between these two functions? void foo& function(int arg, int arg) and void foo function(int& arg, int& arg) and void foo& function(int& arg, int& arg) Tell me if what I think is right. The first one tells you to return a reference to a type foo (probably an object). Does this mean if it did not have it, whatever variable was initialized to that function would be copied, and then returned which wouldn''t do anything? The 2nd one, it passes reference variables so that you can change the actual value of the variables. What''s the difference with doing this with a pointer? I''ve seen not many people use pointers to pass arguments except when using the double pointers. The third one returns a foo type and passes two arguments with reference so they can be changed. Now if this is all right, I''m still not sure when to use what. Except the passsing the reference arguments. I''m not fully understanding what the foo& function () still does. Thanks to anyone who takes the time to read this and help me out! tcache Contact Me ----------- AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Advertisement
Hi Tazel

There is one main difference between passing by reference and passing by pointer: You can't really have a null reference (you can but as I'll explain later it's bad news if you do).

null pointers are used for all kinds of things. Often they are a way of communicating that you don't need the value that the function would normally fill in for you in the object pointed to by the non-null pointer. The function either has to check for the null before writing to it or assume that it will never be passed a null pointer. This can lead to bugs.

Using references avoids this potential for errors. Now you might say you actually want to communicate in this way (ie passing a null). An alternative is to create null versions of your objects (you can't do this for ints and other built in types, so it's limited, but still useful).

Ie if you have a Dog class you could also have a nullDog class, derived from the same base. This can be passed around in places where you would normally have a null pointer but can't because you're using references to increase safety. This is quite an advanced technique but worth mentioning, I feel.

Note:
To get a null reference you would have to dereference a null pointer, which isn't allowed anyway and leads to undefined behaviour. It might crash, it might not. If you're lucky it will crash.

eg
void someFunction(int& r);

int* pInt = 0;
int& rInt = *pInt;//dereferencing a null pointer, might crash!
someFunction(rInt);// bad news

edit: c doesn't have pass by reference - it's a c++ thing

[edited by - petewood on January 18, 2003 11:19:19 AM]
Don't use reference as a replacement to pointers, they are alot more confusing. They have their purpose with operator overloading and container classes, but not with normal function arguments. My first rule-of-thumb is to never use reference on basic types.

If I see this is a piece of code:

int a = 10;
some_function(a);

I, for one, will (correctly IMHO) assume that a is passed by value . Now, when people (ab)use reference, I must go lookup the function declaration/definition to be sure. When you use regular pointers, you are more explicit about what you're doing (which is A Good Thing™ ).



[edited by - CWizard on January 18, 2003 11:26:29 AM]
I thought I''d add some more - answer your questions more than going off in my own little world.

quote:I''m still not sure when to use what


Okay, the main reason for passing things by pointers or references is if the objects are large. By large I mean larger than the built in types. Compilers can cope fine with passing ints, doubles, bools etc. Passing these by reference is a waste of time if the value isn''t going to be changed by the function. If the value needs to be changed then pass it by reference.

If the value isn''t going to be changed then pass built in types by value (ints etc) like in the arguments of your first function.

If the value is an object of user defined type then pass it by reference.

There is then a danger or at least an uncertainty about whether the function will change the value or not. To indicate this have the function use the ''const'' keyword indicate that it won''t change it.

ie
void someFunction(const int& arg1, int& arg2);// yeah your code is a little wrong using arg twice

This should be taken to mean the function can''t change the first argument and will change the second.

As I said in the previous post, I prefer not to use pointers. I only use them for purely C interfaces.
quote:int a = 10;
some_function(a);

I, for one, will (correctly IMHO) assume that a is passed by value


Er... passing by reference is widely used.

If the function had a more realistic name such as

void getWidthAndHeight(int& width, int& height);

Looking at code like that below, would you assume that width and height aren''t being modified?
int width, height;
getWidthAndHeight(width, height);

The names you give functions are important to communicate to people how they should be used. I think your point is bogus (IMHO)
I agree with petewood. If you give your functions and parameters meaningful names, then using references is not a problem. (In my humble opinion anyway. I''m not a professional programmer, so perhaps I''m a bit naive)

Personally I prefer references over pointers wherever it is practical. I like the increased safety. I''ve found it''s a lot easier for bugs to creep in when you''re using pointers. I still use pointers in situations where I feel it is better/simpler, such as the NULL pointer example given.
quote:Original post by petewood
Looking at code like that below, would you assume that width and height aren''t being modified?
int width, height;
getWidthAndHeight(width, height);
Of course I would come to the conclusion that they are passed by reference. But, it does cloud what''s going on.
GetDimension(&width, &height);
Is more explicit, and more natural to my mind. As what I do is passing the address of the variable and not it''s value. In the rest of the language, a plain variable evaluates to it''s value, and so I think the reference syntax is somewhat inconsistent with the rest. I recognize references to be a complementary part of the C++ language, but I see no reasons to use them where pointers do good.

In the end, I think references and pointers are conflicting in a language; use one or the other.

quote:Original post by Oxyacetylene
Personally I prefer references over pointers wherever it is practical. I like the increased safety. I''ve found it''s a lot easier for bugs to creep in when you''re using pointers.
Out of curiosity, in what way are references more safe than pointers? Unless you do nasty casts (which you can do with references as well), which you shouldn''t, I can''t see what''s safer with references.

Your compiler will try it''s damnedest to stop you making an invalid reference.

int & r; // error - reference isn''t initialised
int i;
r = i;

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
quote:in what way are references more safe than pointers


As i''ve already said, you shouldn''t ever get a null reference unless you or the people using your code are incompetant, but I''ll assume they know better than dereferencing a pointer without checking it. It puts the testing for null outside of the function, if necessary.

This topic is closed to new replies.

Advertisement