Reference and Pointer

A

Alf P. Steinbach

* Kuku:
You had problems in answering it??

It's a complete-newbie question, and he isn't.

Why don't you try Google? Or this group's FAQ, perhaps?

Always check the FAQ (and Google) before posting.
 
D

Default User

Martin said:
I have programmed for a couple of months in C and it's this automatic
dereferencing when you pass by reference I find most confusing,
because here C and C++ doesn't work out the same way....

I don't understand what you mean. C has no references, and passing
pointers in C++ works the same way as in C. There is no "it" to work
out differently.



Brian
 
A

Alan Johnson

Default said:
I don't understand what you mean. C has no references, and passing
pointers in C++ works the same way as in C. There is no "it" to work
out differently.

One might try to explain references to a C programmer as "pointers that
automatically dereference themselves". (Yes, I know that isn't accurate
in all contexts.) Based on his comments, Martin Jørgensen is likely
working with that mental model.

Alan
 
D

Default User

Alan said:
One might try to explain references to a C programmer as "pointers
that automatically dereference themselves". (Yes, I know that isn't
accurate in all contexts.) Based on his comments, Martin Jørgensen is
likely working with that mental model.


Which would be fine, but what the heck does "because here C and C++
doesn't work out the same way" mean? Passing pointers works the same.
If he meant that pointers and references aren't the same, then yeah.
Let's stick with that and leave differences between the languages out
of it.



Brian
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Default said:
Alan Johnson wrote:

Exactly... And I can't get it out of my head, but perhaps it will go
away with time :)
Which would be fine, but what the heck does "because here C and C++
doesn't work out the same way" mean? Passing pointers works the same.

I believe in C you would get either a compiler error or warning if you
don't dereference the pointer when you want to access the value of the
pointer (in C). That same thing is also a reference (in C++) and that's
the difference!
If he meant that pointers and references aren't the same, then yeah.

I mean: "Automatic dereferencing" like I wrote. When you write something
with & in front of it, you're actually passing the address of that
variable and not the value of the variable.


Best regards / Med venlig hilsen
Martin Jørgensen
 
P

Phlip

Martin Jørgensen wrote;
Exactly... And I can't get it out of my head, but perhaps it will go away
with time :)

The fix is "a reference is another name for an object; an alias for that
object".

By "object" we also mean items with built-in types...
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Phlip said:
Martin Jørgensen wrote;




The fix is "a reference is another name for an object; an alias for that
object".

By "object" we also mean items with built-in types...

Yeah, I know it... I'm a bit more used to C than C++, but I know it :)


Best regards / Med venlig hilsen
Martin Jørgensen
 
D

Default User

Martin said:
Default User wrote:

I believe in C you would get either a compiler error or warning if
you don't dereference the pointer when you want to access the value
of the pointer (in C).

Huh? Could you give some code example? Pointers work virtually
identically in the two languages. The sole exception I can think of is
automatic conversion of void* to object pointers in C.
That same thing is also a reference (in C++)
and that's the difference!

I don't know what you mean. References are NOT pointers, regardless of
how they might be implemented. There is no pass by reference in C, and
pointers (which are passed by value) work the same in either language.

Somebody's confused. Maybe me.
I mean: "Automatic dereferencing" like I wrote. When you write
something with & in front of it, you're actually passing the address
of that variable and not the value of the variable.

Sure, but what does that have to do with anything? What "difference"
between the languages are you talking about? There are differences
between pointers and references, but that's not a language difference,
other than C ain't got 'em (references).



Brian
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Default User wrote:
-snip-
Sure, but what does that have to do with anything? What "difference"
between the languages are you talking about? There are differences
between pointers and references, but that's not a language difference,
other than C ain't got 'em (references).

If it isn't a language difference, what the hell is it then?


Best regards / Med venlig hilsen
Martin Jørgensen
 
D

Default User

Martin said:
Default User wrote:
-snip-


If it isn't a language difference, what the hell is it then?

You tell me. What difference between the way pointers are handled have
you detected between the two languages.

I'll remind you again, references are not pointers. You're hung up on a
mythical automatic dereferencing that doesn't exist.



Brian
 
K

Kai-Uwe Bux

Default said:
You tell me. What difference between the way pointers are handled have
you detected between the two languages.

I'll remind you again, references are not pointers. You're hung up on a
mythical automatic dereferencing that doesn't exist.

No he isn't. He is just coming to terms with the fact that in C++ you can
write

hm = foo( bar );

and you are not able to tell wether bar is passed by value or by reference,
i.e., when you are calling a function, there is no indication of its side
effects on arguments. The difference between the languages that he is
struggling with is exactly that C++ has call by value *and* call by
reference -- and that the decision is made is at the point of declaration
not at the point of use.

Of course, C++ wouldn't be C++ if life didn't get more complicated: foo
could also be a function object and the call could have side effects there,
too.


Best

Kai-Uwe Bux
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Kai-Uwe Bux said:
Default User wrote: -snip-

Instead of answering the question you're asking me another question - do
you think I'll forget I actually asked you a question, which you
apparently couldn't answer reasonable?

At least you could learn to program in C before you write such bullshit,
so you would actually be aware of the language difference...

Bullshit. First of all:

I can't take you seriously. I *NEVER* wrote that references are
pointers! Grow up, little child and learn to read too!
No he isn't. He is just coming to terms with the fact that in C++ you can
write

hm = foo( bar );

and you are not able to tell wether bar is passed by value or by reference,
i.e., when you are calling a function, there is no indication of its side
effects on arguments. The difference between the languages that he is
struggling with is exactly that C++ has call by value *and* call by
reference -- and that the decision is made is at the point of declaration
not at the point of use.

I think that's true... I still like to think of it this way, as I wrote
before:

In C you would get either a compiler error or warning if you don't
dereference the pointer when you want to access the value of the
pointed-to-value (the pointer in C). That same thing is _a reference_
(in C++).

So in C++ you don't have to dereference when you pass by reference (i.e.
you pass the address of the variable) but you have to do that in C - and
that's CLEARLY a *language* difference! Therefore this is "automatic
dereferencing" seen from a C programmers point of view...

Doesn't matter which way one thinks of it though - as long as the final
result is the same and it works - and I believe this way of "thinking of
it" works for me (i.e. automatic dereferencing), so that's enough for
me. But:

As Philip wrotes:
> The fix is "a reference is another name for an object; an alias for
that object".

And that's just the same thing seen from a different perspective and I
totally agree with him and am aware of that (the last "fix" is easier to
think of in general which I also do sometimes, as I wrote earlier).
Of course, C++ wouldn't be C++ if life didn't get more complicated: foo
could also be a function object and the call could have side effects there,
too.

This difference between the languages is *really* something that I've
been thinking a lot about - and which was a bit hard to accept earlier.
I mean: You can't stop anwering yourself - why doesn't it work the same
way in the two languages??? It just don't.

Conclusion: Well mainly for the OP:

If you also have a background in C like me, and you use references as
arguments to functions in C++ you have to "switch mind" into a different
way of thinking - and the easiest way of understanding it, would
probably be to use "Philip's fix".

As long as you are aware of the difference/similarities I think it's
much easier to avoid mistakes, so that's also why I wrote about this
"difference".

If you don't have a background in C, there's probably no confusion at
all and then you shouldn't worry...


Best regards / Med venlig hilsen
Martin Jørgensen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top