Advertisement

& and * and all that jazz

Started by July 22, 2000 06:42 PM
11 comments, last by cow_in_the_well 24 years, 5 months ago
Is there any difference between void foo(int *number); and void foo(int &number); ??? I have a feeling that they both do the same thing.... ----------------------------- -cow_in_the_well http://cowswell.gda.ods.org/ "If it's stupid but works, it's not stupid" - Murphey's Law

- Thomas Cowellwebsite | journal | engine video

void foo(int *number); recieves a pointer to an integer, while the other one recieves a reference to it. A pointer is holds the address of a variable, while a reference is an alias for the variable name. There is not much noticable diff. but they are diff.

*** Triality ***
*** Triality ***
Advertisement
Well, they can DO the same thing, but in the first function number is a pointer, and in the second it''s just an int.

lntakitopi@aol.com - http://www.geocities.com/guanajam
an important diff is that you cant really pass null to a reference so you dont have to check that it is a valid pointer.


JS
http://www.thepeel.com/void
JShttp://www.thepeel.com/void
But neither of them creates a copy of number?

-----------------------------
-cow_in_the_well

http://cowswell.gda.ods.org/


"If it's stupid but works, it's not stupid" - Murphey's Law

- Thomas Cowellwebsite | journal | engine video

quote: Original post by JS

an important diff is that you cant really pass null to a reference so you dont have to check that it is a valid pointer.

No, you can - and very easy:

foo( *(int*) NULL );

You just don''t have to worry about it in the function.
Advertisement
The first is C''s way of manually constructing a call by reference, using a pointer. That is, when you call this function you will pass an address.

The second is C++''s reference parameter operator, you can call it without using any operator and can access it in the function without needing special operators, it will automatically obtain the address for you.

I''m not sure that is totally correct, but I think the idea is right.
I''m sure I''ll be corrected if null_pointer comes along

-Mezz
That''s right!
And references are VERY important for operator overloading.
Yep!
Those guys are perfectly right.
If you still don''t understand it, you should read a book about c++ (stroustroups if you''re a hard one), or alternatively search through the forums here; it was explained *soooo* many times...

pi~

*jazz... mmm... i like jazz...*
Jan PieczkowskiBrainwave Studios
Serge K, could you check out My Question and give me a few guidelines, since you seem to know that stuff pretty good! Thank you!

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

This topic is closed to new replies.

Advertisement