Reference and Pointer

K

Kai-Uwe Bux

Martin said:
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.

Well, as soon as you offer in this forum the way of thinking that works for
you as an explanation (to others), you have to be prepared that you might
be criticized if your explanations are regarded sloppy or misleading.

I think the following two paragraphs explain why you are getting yourself
into some deep waters in this group:

Martin Jørgensen wrote:
[snip]
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++).

Parsed literally, this is not true: "That same thing" is actually the same
in C++: if you try to access the value of the pointed-to-value, you have to
dereference the pointer. No difference.

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...

This also does not quite parse: the elliptic sentence "but you have to do
that in C" expands to:

In C you have to dereference when you pass by reference.

Now that does no make sense since, in C you cannot pass by reference.


Relax: I understand what you mean and I also understand that, how, and why
this picture works for you. But please accept that as soon as you put it
into words and offer it on this group, people will take your words apart
and point out technical and logical inaccuracies.

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

Right. Lucky me.


Best

Kai-Uwe Bux
 
P

Phlip

Kai-Uwe Bux said:
I think the following two paragraphs explain why you are getting yourself
into some deep waters in this group:

"Argumentum ad populum" noted. Nobody is busting on Martin here except you.
Martin Jørgensen wrote:
Parsed literally, this is not true: "That same thing" is actually the same
in C++: if you try to access the value of the pointed-to-value, you have
to
dereference the pointer. No difference.

Martin's statement is simply ambiguous, and he obviously knows the correct
version. If you gracefully accept the interpretation that's closer to C++
canon, then the thread can easily progress.

When someone is learning how to write about a language, and they write an
ambiguous sentence, politely coaching them to correct the ambiguity is much
better than repeatedly accusing them of the wrong interpretation. Fun though
it is.
 
K

Kai-Uwe Bux

Phlip said:
"Argumentum ad populum" noted. Nobody is busting on Martin here except
you.

Are you really addressing me? The heat was mainly between Default User and
Martin, not me. If you reread the thread you will find that I am not
busting Martin at all. All I tried was to explain which kind of words on
Martins part triggered the responses he got (from others).

Also, "argumentum ad populum" would be "you are wrong because the majority
here says so". This is not what I argued. I argued that writing things that
do not quite come out exactly right run a high risk is this group of
triggering harsh criticism. The direction of inference/explanation that I
was using is exactly the opposite to argumentum ad populum.

Martin's statement is simply ambiguous, and he obviously knows the correct
version. If you gracefully accept the interpretation that's closer to C++
canon, then the thread can easily progress.

When someone is learning how to write about a language, and they write an
ambiguous sentence, politely coaching them to correct the ambiguity is
much better than repeatedly accusing them of the wrong interpretation. Fun
though it is.

"politely coaching them to correct the ambiguity" is exactly what I did.
Reread *my* posts and if you can, point out a place where I was impolite.


Best

Kai-Uwe Bux
 
?

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

Kai-Uwe Bux said:
Well, as soon as you offer in this forum the way of thinking that works for
you as an explanation (to others), you have to be prepared that you might
be criticized if your explanations are regarded sloppy or misleading.

I think the following two paragraphs explain why you are getting yourself
into some deep waters in this group:

I don't think I'm into some deep water, but lets discuss it then.
Martin Jørgensen wrote:
[snip]
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++).


Parsed literally, this is not true: "That same thing" is actually the same
in C++: if you try to access the value of the pointed-to-value, you have to

No. "That same thing" is not the same in C as in C++. In the C++
language it's a reference! "Default user" even agrees with me a bit,
since earlier he wrote that: "There is no pass by reference in C".
dereference the pointer. No difference.

I can't believe you're actually writing that there isn't any difference!

I'm writing that there *IS* a very clear difference. I don't think you
read it correctly. We're discussing using the &-operator. And if you
think it works the same way in C as in C++, when you pass a variable
into a function with the &-operator, then you're completely wrong.

Let me point this completely out for you:

1) In C you pass the address of the variable value.
2) In C++ You pass the value by reference.

I've already explained this and other people seem to have understood it
completely correctly and as I believe I also previously explained it.

And you don't see any difference?

OMFG... Not strange that you can't see that in C you would need to
dereference a pointer (containing the address of the variable) while in
C++ it's like having "automatic dereferencing" of a pointer-to-variable,
so to speak - in C-terms...

In general: You really need to understand this difference, if you want
to understand the whole idea here...
This also does not quite parse: the elliptic sentence "but you have to do
that in C" expands to:

In C you have to dereference when you pass by reference.

What you're writing doesn't make any sense! I'm not writing whatever you
claim with the above, since it's completely nonsense!
Now that does no make sense since, in C you cannot pass by reference.

If it doesn't make any sense, then why did you write this nonsense then,
in the first place? I don't think I ever wrote this nonsense that you
claim I wrote.
Relax: I understand what you mean and I also understand that, how, and why
this picture works for you. But please accept that as soon as you put it

I don't think you understand it, because what you're writing is nonsense
and I can't recognize that what you're writing about should have
originated from my posts...
into words and offer it on this group, people will take your words apart
and point out technical and logical inaccuracies.

I only see 2 persons in this thread that claims that C and C++ works out
completely the same way in this discussion (regarding dereferencing /
"automatic derefencing"):

You and "Default user". I believe other people understand the point...

I can't see these "logical inaccuracies" you're talking about, although
I can see that your last post contains some nonsense - I think you
misread what I wrote somewhere earlier...

This is however unclear to me: I'm not sure about where it is that you
didn't understood or agreed to what I wrote earlier - so where is it,
precisely???

But ofcourse it's no big deal if something was inaccurate written -
you're just fundamentally wrong when you say that there no difference
between passing something to a function in C and C++. And I can't
believe you didn't understood that, because I think I wrote it pretty
clearly previously...
Right. Lucky me.

I think it's nice to understand the difference if the OP have a
background in C and want to learn C++, exactly like myself... That's why
I brought the topic up in the first place.

It's no big deal however since understanding "pass-by-value" and
"pass-by-reference" isn't "rocket science" or very difficult in the
first place, so it's just another perspective on the "Reference vs.
pointer"-discussion - accordingly to the thread subject...


Best regards / Med venlig hilsen
Martin Jørgensen
 
P

Phlip

Kai-Uwe Bux said:
Also, "argumentum ad populum" would be "you are wrong because the majority
here says so". This is not what I argued. I argued that writing things
that
do not quite come out exactly right run a high risk is this group of
triggering harsh criticism. The direction of inference/explanation that I
was using is exactly the opposite to argumentum ad populum.

You started with the assumption that any quorum here though Martin was
wrong.

However, consider my flames^W attempts to help retracted from you and
directed at Default User! Mostly. ;-)
 
D

Default User

Kai-Uwe Bux said:
yourself >> into some deep waters in this group:

Are you really addressing me? The heat was mainly between Default
User and Martin, not me. If you reread the thread you will find that
I am not busting Martin at all. All I tried was to explain which kind
of words on Martins part triggered the responses he got (from others).

I'm not angry with nor attempting to rail on Martin. In my view, he has
some confused ideas (although quite possibly it is a failure by me to
understand his ideas). I'm attempting to get him to see what I consider
the more correct way of looking at the situation. In particular, to
separate references and pointers in his mind. References are
references, and have their own syntax, what pointers do or do not do
shouldn't be a concern.

Pointers and references have their own particular use cases, although
various people disagree where that dividing line is sometimes.

I've been concerned about his statements about differences between C
and C++, because I thought he had a misconception about how things
worked. He just needs to leave C out of it, as it really doesn't come
into the picture. In this case, C really is a subset. It doesn't have
references, and pointers work the same way in either language.



Brian
 
D

Default User

Martin said:
No. "That same thing" is not the same in C as in C++. In the C++
language it's a reference! "Default user" even agrees with me a bit,
since earlier he wrote that: "There is no pass by reference in C".

That was a factual statement. It has a corollary statement, "pointers
work the same way in the two languages."

In C, "pass by reference" is somewhat simulated by passing (by value)
pointers. The very same method can be used with no difference in C++.
C++ then also has a separate method for actual passing by reference,
references.

I can't believe you're actually writing that there isn't any
difference!

There is no difference.
I'm writing that there IS a very clear difference. I don't think you
read it correctly. We're discussing using the &-operator. And if you
think it works the same way in C as in C++, when you pass a variable
into a function with the &-operator, then you're completely wrong.

There is no difference in the way the address-of (&) operator works. It
so happens that the symbol & is also used in reference declarations.
It's a different thing though.
Let me point this completely out for you:

1) In C you pass the address of the variable value.
2) In C++ You pass the value by reference.

Or in C++ you pass the address of the variable. Perfectly legal.
I've already explained this and other people seem to have understood
it completely correctly and as I believe I also previously explained
it.

And you don't see any difference?

OMFG... Not strange that you can't see that in C you would need to
dereference a pointer (containing the address of the variable) while
in C++ it's like having "automatic dereferencing" of a
pointer-to-variable, so to speak - in C-terms...

Well, "like" to a certain degree.
I only see 2 persons in this thread that claims that C and C++ works
out completely the same way in this discussion (regarding
dereferencing / "automatic derefencing"):

You and "Default user". I believe other people understand the point...

I believe that is a misrepresentation of what I've said.

I think it's nice to understand the difference if the OP have a
background in C and want to learn C++, exactly like myself... That's
why I brought the topic up in the first place.

I DO have a background in C. I still haven't figured out the
differences you think you see, other than C doesn't have references.




Brian
 
K

Kai-Uwe Bux

Default said:
I'm not angry with nor attempting to rail on Martin.

I understand that. Same here. But his posts show, I think, that he feels
differently about our posts. Conceivably, some words in our posts could be
taken the wrong way.


[parts of agreement snipped]


Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Phlip said:
You started with the assumption that any quorum here though Martin was
wrong.

Really -- where? I think, I just noted that he received criticism. No
counting involved. Then, I figured which formulations triggered the
criticism and tried to straighten them out. Deplorably, Martins latest
response to my post shows that I did not succeed. Maybe, you could try
"politely coaching them to correct the ambiguity".


[snip]

Best

Kai-Uwe Bux
 
I

int2str

Martin said:
I'm writing that there *IS* a very clear difference. I don't think you
read it correctly. We're discussing using the &-operator. And if you
think it works the same way in C as in C++, when you pass a variable
into a function with the &-operator, then you're completely wrong.

Let me point this completely out for you:

1) In C you pass the address of the variable value.
2) In C++ You pass the value by reference.

I believe the issue is where you put the "& operator" (as you call it).
Also when discussing this from a C++ point of you, you need to
introduce slightly different teminalogy.

There is _NO_ difference betwen C and C++ in the following regards:

1) Pass by value - same in C and C++

Example:
void foo( int i );
...
int x = 5;
foo( x );

2) Pass by pointer - same in C and C++

Example
void foo( int *i ) { *i = 5; }
...
int x = 10;
foo( &x );

Now there is a big difference between C and C++ in regards to "pass by
reference": It simply doesn't exist in C.

Example:
void foo( int & i ) { i = 10; }
...
int x = 10;
foo( x ); // <-- no "&" at call time

Where you seem to use the wrong terminalogy is that you insist there is
"automatic dereferencing" going on in the "pass by reference". But
you're not passing in a pointer. There is nothing to "dereference". You
are passing in an alias to the object, not a pointer.

In C++, if a function is declared thusly:
void foo( int & i );

You CANNOT call it like this:
int x = 10;
foo( &x );

Since the question was asked about C++ in a C++ newsgroup. You have to
distinguish and define the three methods separately.

Cheers,
Andre
 
D

Default User

Kai-Uwe Bux said:
Default User wrote:

I understand that. Same here. But his posts show, I think, that he
feels differently about our posts. Conceivably, some words in our
posts could be taken the wrong way.

I'm sure. As always, it's an impersonal medium at best. At worst, every
little instance of poor wording can rankle or seem like an attack.

I believe there's plenty of violent agreement and a modicum of
miscommunication going on.




Brian
 
K

Kai-Uwe Bux

Martin said:
I don't think I'm into some deep water, but lets discuss it then.

Ok. However, we seem to have a certain difficulty in communicating. Please
keep in mind that nothing I wrote nor anything I am about to write is meant
to offend you. All I am trying is to get certain wordings straight.

Martin Jørgensen wrote:
[snip]
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++).

Ok, these were your original words. I would like to start with the
observation that the first sentence is true and stays true if you replace C
by C++:

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++).

Therefore, the first sentence is a little bit awkward a sentence to start a
paragraph that is meant to pinpoint a difference between C an C++.

Now the second sentence: "That same thing is a reference in C++". For this
one, I just would like to ask what the phrase "That same thing" is supposed
to refer to. It should be something mentioned in the first sentence, right?
Ok. Make your pick and find something in the first sentence that completes
the pattern


_________________ is a reference in C++.


I cannot find anything mentioned in the first sentence that matches this
requirement. The natural candidate would be the phrase "the pointer in C".
Then we would read:


the pointer in C is a reference in C++.

Now that line is odd, because C++ has pointers and those pointers work
exactly the same like pointers in C. So a pointer in C really corresponds
to a pointer in C++ and a reference in C++ simply has no counterpart in C.

No. "That same thing" is not the same in C as in C++. In the C++
language it's a reference! "Default user" even agrees with me a bit,
since earlier he wrote that: "There is no pass by reference in C".

Clearly, I did not understand what the phrase "That same thing" was supposed
to refer to.

I can't believe you're actually writing that there isn't any difference!

Please, do no exaggerate my claims. Of course the is a difference between C
and C++. And we already agreed on a wording to describe it:

C++ has call by value and call by reference whereas C only has
call by value.

All the fuzz is just about the *additional* explanations that you propose.

I'm writing that there *IS* a very clear difference. I don't think you
read it correctly. We're discussing using the &-operator. And if you
think it works the same way in C as in C++, when you pass a variable
into a function with the &-operator, then you're completely wrong.

Hm, the only difference in how the &-operator *works* in C and C++ is that
you can overload operator&() in C++. The workings of the &-operator are
exactly the same: in both languages, it returns a pointer to the object
specified by its argument.

In particular, *when you pass a variable into a function with the
&-operator*, there is no difference in the C and C++ will handle this
function call (unless operator& was overloaded in C++). The difference
between the two languages is not something that shows *when you pass a
variable into a function with the &-operator*.
Let me point this completely out for you:

1) In C you pass the address of the variable value.
2) In C++ You pass the value by reference.

Incomplete. Instead it should read:

If you want to call a function that modifies an object from the ambient
scope:
1) In C you pass the address of the object.
2) In C++ you do the same, or alternatively, you pass a reference for
the object. Which way you have to go is determined by the signature.


[snip]
And you don't see any difference?

I do. See above. The question is not *whether* there is a difference, but
how to phrase it.
OMFG... Not strange that you can't see that in C you would need to
dereference a pointer (containing the address of the variable) while in
C++ it's like having "automatic dereferencing" of a pointer-to-variable,
so to speak - in C-terms...

I do see that. I just don't find that wording particularly helpful.
Moreover, this mental model led you to claims like: there is a difference
in how operator& works in C and C++ (besides its capacity for being
overloaded). Such claim is just false and a mental model leading to such
statements is of questionable value.


[snip]


But ofcourse it's no big deal if something was inaccurate written -
you're just fundamentally wrong when you say that there no difference
between passing something to a function in C and C++.

I did not say that. I wrote there is no difference with regard to the
following statement:

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).

I do not think that one should explain a language *difference* using
sentences about C that just literally stay true when you replace C with
C++.


Best

Kai-Uwe Bux
 
?

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

Martin Jørgensen wrote: -snip-

Where you seem to use the wrong terminalogy is that you insist there is
"automatic dereferencing" going on in the "pass by reference". But
you're not passing in a pointer. There is nothing to "dereference". You
are passing in an alias to the object, not a pointer.

I really can't understand that. If you pass the address of a variable
then you *need* to dereference it, so get the value of that address.
In C++, if a function is declared thusly:
void foo( int & i );

You CANNOT call it like this:
int x = 10;
foo( &x );

I never wrote that.


Best regards / Med venlig hilsen
Martin Jørgensen
 
?

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

Kai-Uwe Bux said:
Martin Jørgensen wrote:
-snip-



Ok, these were your original words. I would like to start with the
observation that the first sentence is true and stays true if you replace C
by C++:

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++).

I don't see why you want to replace C by C++. Can you tell me why? I
don't see any problem with the original construction of the words...
Therefore, the first sentence is a little bit awkward a sentence to start a
paragraph that is meant to pinpoint a difference between C an C++.

I can't see that.
Now the second sentence: "That same thing is a reference in C++". For this
one, I just would like to ask what the phrase "That same thing" is supposed
to refer to. It should be something mentioned in the first sentence, right?

Yes! The pointer points to the address of the variable that is passed to
some function... That's why this sign "&" means the "address-of-operator".

And that explanation fully agrees with the the definition of "&":

"Definition: The address of operator, &, returns the address of a
variable or object.

Example:

int result; // Integer object.
int *pt; //Integer pointer
pt = &result; //Stores the address of result in the pointer, pt."

from http://cplus.about.com/od/cprogrammin1/l/bldef_addofoper.htm

I already wrote this a couple of times, I believe - I feel just like
repeating myself over-and-over.

So: You need to dereference this pointer variable, in order to "access
the value of the pointed-to-value (the pointer in C)."

I can't see that should be so difficult to understand. Now, in C++ you
can pass arguments in a function header "by-reference" by using the
"&"-operator. You can have a function header like this:

return_type some_function( int &reference ) {..... }

and the "&reference" in cpp means: "accept a reference which is just
like an alias for the passed-by-variable".

C doesn't understands references. If it should accept a pointer it would be:

return_type some_function( int *reference ) {..... }

In the C language *THAT SAME THING* (remember my previously
explanations?): "&reference" means: "dont access the variable itself
directly. Instead pass the address of the variable, so it'll become a
pointer-to-variable."

You need to *DEREFERENCE* any pointer-to-variable in order to access the
values of those variables in C.

You *can't* have a construction like this in C:

return_type some_function( int &reference ) {..... }

So don't tell me that there isn't any difference between the 2
languages, like I thought somebody wrote earlier (could be that was just
some kind of "communication error", since I think everyone at least at
some moment agreed to this fact even though they wrote that there isn't
any difference - not easy to understand that)...

I'm not sure if this is a detailed enough explanation, but I really feel
really like I'm repeating myself too much... Still you should understand
it now, right?
Ok. Make your pick and find something in the first sentence that completes
the pattern


_________________ is a reference in C++.

I don't know what you're talking about, and I really don't feel like
playing silly games right now. Why don't you just tell whatever you
think instead of asking me to guess what you think (I can't guess what
you think anyway)?
I cannot find anything mentioned in the first sentence that matches this
requirement. The natural candidate would be the phrase "the pointer in C".
Then we would read:


the pointer in C is a reference in C++.

The pointer in C *dereferenced* corresponds to a reference in C++!
Now that line is odd, because C++ has pointers and those pointers work
exactly the same like pointers in C. So a pointer in C really corresponds
to a pointer in C++ and a reference in C++ simply has no counterpart in C.

The pointer in C *dereferenced* corresponds to a reference in C++!

That's what the whole discussion is all about, remember that?

You remember this talk about "automatic dereferencing"? What do you
think that talk was about? You couldn't have understood that could you?
That is fully consistent with my previous explanation, AFAICS. If you
think I somewhere wrote something wrong, perhaps you can tell me where
this is because I have a very hard time figuring out what you mean...

Still, you need to understand that this "&reference" isn't the variable
itself - it's the *address* of that variable. I don't think you
understood that from my previously explanation, but now I've given an
example above together with a pretty detailed explanation, so I think it
should be possible to understand it now.
Clearly, I did not understand what the phrase "That same thing" was supposed
to refer to.

You should just ask about it then, because you're really confusing me
with your guesses.
Please, do no exaggerate my claims. Of course the is a difference between C
and C++. And we already agreed on a wording to describe it:

I'm not sure if I exaggerated. You and "default user" both specifically
wrote something about "No difference between the 2 languages", but
instead you ought to express that ofcourse there is a very clear
difference between C and C++.

That would show that you at least understood something relevant in my
explanations...

So I *really* don't understand all of this talk with "no difference".

Like: When there is no difference between passing a pointer, why the
h*** do you then write about it as if it was relevant, when it's
absolutely *irrelevant*?

I just don't understand that... It's just making the discussion
*unnecessary* long and complicated and nobody is interested in that, I hope.
C++ has call by value and call by reference whereas C only has
call by value.

All the fuzz is just about the *additional* explanations that you propose.

It's *really* simple...
Hm, the only difference in how the &-operator *works* in C and C++ is that
you can overload operator&() in C++. The workings of the &-operator are
exactly the same: in both languages, it returns a pointer to the object
specified by its argument.

Irrelevant, I believe.
In particular, *when you pass a variable into a function with the
&-operator*, there is no difference in the C and C++ will handle this
function call (unless operator& was overloaded in C++). The difference
between the two languages is not something that shows *when you pass a
variable into a function with the &-operator*.

Irrelevant, I believe.

I believe that you ought not to bring all of these irrelevant topics
into the discussion, as they're just confusing more than they serve any
reasonable purpose.
Incomplete. Instead it should read:

If you want to call a function that modifies an object from the ambient
scope:

That discussion is irrelevant, AFAICS.
1) In C you pass the address of the object.
2) In C++ you do the same, or alternatively, you pass a reference for
the object. Which way you have to go is determined by the signature.

I don't disagree, but my explanation was enough for understanding and
realizing that there is a difference between C and C++, so what you're
writing is simply irrelevant and not something I intend to waste much
time on...
[snip]

And you don't see any difference?


I do. See above. The question is not *whether* there is a difference, but
how to phrase it.

You should try to concentrate on what is relevant here.
I do see that. I just don't find that wording particularly helpful.

Somehow I don't think you can see it even though you write it... Are you
sure you can see it, with my last 3 postings with pretty long explanations?
Moreover, this mental model led you to claims like: there is a difference
in how operator& works in C and C++ (besides its capacity for being

Wrong! I still don't understand all of your irrelevant talk about "how
operator& works in C and C++".

That's just irrelevant garbage... Where in my previous posts did you see
I wrote something about operator& ???

You need to cut down all this irrelevant talk... It's just confusing...

I suppose you just don't understand something in the previously
explanations and are making wild guesses... Please stop guessing,
because it's too confusing to read.
overloaded). Such claim is just false and a mental model leading to such
statements is of questionable value.


[snip]



But ofcourse it's no big deal if something was inaccurate written -
you're just fundamentally wrong when you say that there no difference
between passing something to a function in C and C++.


I did not say that. I wrote there is no difference with regard to the
following statement:

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).

I really don't understand you... Sometimes you write that there isn't
any difference between the 2 languages. Like with the above statement.

But I clearly pointed out the difference I believe... And yet you don't
think there's a difference?
I do not think that one should explain a language *difference* using
sentences about C that just literally stay true when you replace C with
C++.

I dont understand one word of that. I never wanted to replace C with C++
(or was it the other way around?). You are the only one who talks about
replacing C and C++...

As I wrote earlier: You really need to cut down this irrelevant talk,
because some of it is really confusing...


Best regards / Med venlig hilsen
Martin Jørgensen
 
D

Default User

Martin Jørgensen wrote:

I'm not sure if I exaggerated. You and "default user" both
specifically wrote something about "No difference between the 2
languages", but instead you ought to express that ofcourse there is a
very clear difference between C and C++.

Please quote where I said that. I didn't. I said there was no
difference in the way pointers work between the two. Please STOP
claiming I said things I didn't. I clearly stated that, of course, C
doesn't have references AT ALL.

As I said, you need to get it out of your head that pointers and
references are in any way linked. They aren't, other than in a loosely
conceptual way (and probably in the implementation, but that's not of
interest).

Aliasing is the better conceptual model for references, that gets you
away from this, "in C if you don't dereference the pointer it's an
error" business. It has nothing to do with anything. References are not
pointers, pointers are not references.




Brian
 
J

Jakob Bieling

Martin Jørgensen said:
"Definition: The address of operator, &, returns the address of a
variable or object.
Now, in C++ you
can pass arguments in a function header "by-reference" by using the
"&"-operator. You can have a function header like this:

No. This is not the address-of operator. Just like the '&' below is
not the address-of operator either.

int a = 1;
int b = 1;

if (a &b) {}

So it depends on the context, what a character means.
and the "&reference" in cpp means: "accept a reference which is just
like an alias for the passed-by-variable".

C doesn't understands references. If it should accept a pointer it
would be:
return_type some_function( int *reference ) {..... }

In the C language *THAT SAME THING* (remember my previously
explanations?): "&reference" means: "dont access the variable itself
directly. Instead pass the address of the variable, so it'll become a
pointer-to-variable."

I think there is a misconception about what the characters
'&reference' mean:

When you just say '&var', where 'var' is an _existing_ variable, you
will always[*] (in C and in C++) get the address of that variable.
There, the '&' is the address-of operator applied to a variable.

When you say 'int &var', '&' is _not_ the address-of operator
anymore. It is part of the type, which is why I would have written 'int&
var' in the first place. The '&' in this context means something
completely different, because you are using a different concept. It is
_not_ the address of anything.

I'll just go into how _I_ see references. Maybe this helps you
understand the views of 'Default User' and Kai-Uwe.

Reference means you want another name for an object. References are
not addresses. They are a different concept:

int i = 0;
int j = 0;
int& k = i;

It is clear that 'i' does not have anything to do with an address.
'i' is just a name for a sequence of bytes in memory. 'j' is also a name
of a sequence of bytes in memory, but for a different one. Now, 'k' is a
_different_name_ for the _same_sequence_ of bytes in memory, that 'i' is
a name for.

[*] Let's keep overloaded operators out of this .. simplifies my
wording :)
[..] "&reference" isn't the
variable itself - it's the *address* of that variable.

See above.

regards
 
T

Tomás

Kuku posted:
What is the difference between a reference and a pointer?


The thing you have to understand is that there's a lot of "magic" in C++.
There's lots of stuff in C++ that just gets done for you! Examples:

1) Virtual Functions

As many of us know, compilers implement virtual functions by using pointers
to V-Tables which they "hide" at the beginning of the object in question.
However, as a C++ programmer, we don't need to know anything about this, we
just need to understand its behaviour! Furthermore, the C++ Standard leaves
it up to the compiler to implement this feature in WHATEVER WAY IT PLEASES.
The compiler isn't obligated to use V-Tables, and concordantly, if your code
contains "hacks" which plays with a V-Table, it isn't guaranteed to work on
every platform and implementation.

2) References

Again, it's irrelevant how the compiler actually implements references --
all you need to understand is their behaviour, which can be split into three
categories:

2.a) Simple statements like as follows:

int k;

int &r = k;

r = 7;

In this example, the compiler will just treat "r" as if you had written "k".

2.b) Passing function arguments by reference:

The idea here is that you're actually changing the object in the calling
function. This seems to be where most of you have a problem. All you need to
understand and know is the behaviour of this facility. In the background,
the compiler may very well be passing around the address of an object (it
may also be teaching elephants how to drive if it wants), but none of this
concerns you as the programmer. Just come to terms with its behaviour.

2.c) Returning a reference:

std::string& str = ReturnStringByRef();

str += "monkey";


In this example, all you need to know is that when you change str, you're
actually working with the object which was returned by reference from
"ReturnStringByRef". The object in question may be a global variable, or it
may be a static variable in "ReturnStringByRef", or it may have be
dynamically allocated. It's very possible (likely, even) that a compiler
achieves this behaviour by passing around an object's address, but that's
irrelevant to you as the programmer.

Some people don't like magic in computer programming languages. For those of
you here that regularly program in C++, have you ever had a look through a C
program and realised just how "manual" it is! Nothing is done magically!

If you don't like magic, you can still use C++, but just don't use any of
the modern features -- you can stick to the techniques you used in C. I can
assure you however, that you will come to see the great merit in all the
magic there is in C++. It's much handier to type the keyword "virtual", than
to have to go create, design and implement a V-Table.

Moral of the story is this: Get to know references. Get to know how they
work. Analyse every aspect of their behaviour. When you become accustomed to
exactly how they work (and accept that magic is happening in places), then
you won't bother asking questions, because you realise that the compiler is
free to achieve things in any way it pleases.

Notwithstanding this though, there are people in this world who refuse to
just "skim the surface", (I'd classify myself as one of these), and who
demand to understand exactly what's going on. In order to satisfy my hunger,
I went about writing a C++ program which had all sorts of features which
were magic, and then tried to rewrite the program in C. For virtual
functions, I wrote my own V-Tables. For pass-by-reference, I passed
pointers. For return-by-reference, I returned pointers.
If you really want to understand what goes on under the hood, you may
appreciate undertaking such a pet project -- it will greatly enhance your
understanding of C++.

But when you're in Rome, do as the Romans do; and when you're programming in
C++, exploit all the magic that comes your way. So pass object's by
reference if you want to alter them! Use pointers when you actually want to
mess with memory addresses (like iterating through the characters of a
string).

-Tomás
 
?

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

Default said:
Martin Jørgensen wrote: -snip-

As I said, you need to get it out of your head that pointers and
references are in any way linked. They aren't, other than in a loosely

I DIDN'T WRITE THEY WERE LINKED... Okay?
conceptual way (and probably in the implementation, but that's not of
interest).

Aliasing is the better conceptual model for references, that gets you
away from this, "in C if you don't dereference the pointer it's an
error" business. It has nothing to do with anything. References are not
pointers, pointers are not references.

I DIDN'T WRITE THAT! Okay?

You're about to enter my kill-filter if you don't stop claiming I wrote
that references = pointers.


Best regards / Med venlig hilsen
Martin Jørgensen
 
?

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

Jakob said:
No. This is not the address-of operator. Just like the '&' below is
not the address-of operator either.

When you write: "No. This is not the address-of operator....
bla.bla.bla", then that's completely ridiciculous because I even
distinguished clearly between that myself in my previous explanation.
int a = 1;
int b = 1;

if (a &b) {}

So it depends on the context, what a character means.

and the "&reference" in cpp means: "accept a reference which is just
like an alias for the passed-by-variable".

C doesn't understands references. If it should accept a pointer it
would be:
return_type some_function( int *reference ) {..... }

In the C language *THAT SAME THING* (remember my previously
explanations?): "&reference" means: "dont access the variable itself
directly. Instead pass the address of the variable, so it'll become a
pointer-to-variable."


I think there is a misconception about what the characters
'&reference' mean:

When you just say '&var', where 'var' is an _existing_ variable, you
will always[*] (in C and in C++) get the address of that variable.

OMFG! That's what I wrote...
There, the '&' is the address-of operator applied to a variable.

Please stop repeating what I write, if you don't have anything relevant
to disagree with...
When you say 'int &var', '&' is _not_ the address-of operator
anymore. It is part of the type, which is why I would have written 'int&

OMFG! That's exactly what I wrote!
var' in the first place. The '&' in this context means something
completely different, because you are using a different concept. It is
_not_ the address of anything.

IT IS IN C! Notice how I *very clearly* distinguish between the two
languages, goddamnit... You're making something up, which I never even
wrote.
I'll just go into how _I_ see references. Maybe this helps you
understand the views of 'Default User' and Kai-Uwe.

I think they understand... They should after all these long
explanations. And you should too.
Reference means you want another name for an object. References are
not addresses. They are a different concept:

That exactly corresponds to what I wrote. So what's your point? Why the
hell are you just repeating my explanation, in your own words?
int i = 0;
int j = 0;
int& k = i;

It is clear that 'i' does not have anything to do with an address.

I never wrote that!
'i' is just a name for a sequence of bytes in memory. 'j' is also a name
of a sequence of bytes in memory, but for a different one. Now, 'k' is a
_different_name_ for the _same_sequence_ of bytes in memory, that 'i' is
a name for.

That exactly corresponds to what I wrote. So what's your point? Why the
hell are you just repeating my explanation, in your own words?
[*] Let's keep overloaded operators out of this .. simplifies my
wording :)

Let me be very clear here: There's *absolutely* no reason to discuss
overloaded operators here.
[..] "&reference" isn't the
variable itself - it's the *address* of that variable.


See above.

What I don't understand is why the h** you seem to disagree with me and
yet you're just repeating my whole explanation in your own words?

If I should cut everything down of what you wrote, then you completely
agree with me because it's just garbage you're writing about in this post...

There's absolutely nothing of your criticism that I regognize... Your
whole post is ridiculous...


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