What's the difference of those two ways of passing parameters

W

wudehui2006

hi

For instance for a function
void func(MyClass *pMyClass)
void func(MyClass& MyClass)
What's the difference of two? The first pass the address of the
MyClass,
how about the 2nd one?
usually which one is preferred?
Thanks a lot!
 
K

Keith Thompson

For instance for a function
void func(MyClass *pMyClass)
void func(MyClass& MyClass)
What's the difference of two? The first pass the address of the
MyClass,
how about the 2nd one?
usually which one is preferred?
Thanks a lot!

The second form is C++; it's a syntax error in C.

For more information, try comp.lang.c++ -- but first take a look at
<http://www.parashift.com/c++-faq-lite/> and/or any decent C++
reference.
 
M

Martin Ambuhl

void func(MyClass *pMyClass)
void func(MyClass& MyClass)
What's the difference of two?

Assuming that the type MyClass is defined somewhere and that each of
these is followed by ';' (prototypes) or { /* something */ }
(definitions), the first is legal C and the second is a syntax error.

If this is not an adequate answer to your question, try posting to a
newsgroup for the language that you are actually using.
 
K

Keith Thompson

BartC said:
The second style has limited availability in C language, for example
lcc-win32 compiler allows it.

If the OP needs C code that will work anywhere, then style one is better.

The second style has *no* availability in the C language. If
lcc-win32 allows it, it's an extension, not part of the language. Not
that there's anything wrong with that.

I see "Antoninus Twink" hasn't changed. To the original poster: he's
advising you to post your C++ questions to comp.lang.c, ignoring the
fact that there's a perfectly good comp.lang.c++ newsgroup. He's
deliberately trying to mislead you.
 
C

CBFalconer

BartC said:
The second style has limited availability in C language, for
example lcc-win32 compiler allows it.

If it does that means that lcc-win32 doesn't detect the syntax
error.

Twink is a troll, and should be totally ignored.
 
I

Ian Collins

George said:
Keith, you may know Antoninus Twink better than I do, but it occurs to me
that we each may have a variety of perspectives that we can choose. In
your chosen perspective he's encouraging people to post C++ questions to a
C newsgroup. However the perspective I chose after viewing his message
(which was based on an initial impression and perspective like yours I
admit) was that I don't know his true motivations, and any perspective
could be wrong.

You just have to look at twinky's posting history. It's always
encouraging people to follow up off topic posts here. This does the OP
a disservice by not directing them to the best forum for their question.
 
P

Phil Carmody

George Peter Staplin said:
Keith, you may know Antoninus Twink better than I do, but it occurs to me
that we each may have a variety of perspectives that we can choose. In
your chosen perspective he's encouraging people to post C++ questions to a
C newsgroup.

You mean the way that he provided a link to a C++ document, and
then said "Feel free to post again" about it?
However the perspective I chose after viewing his message

is fucked up. Don't be so wilfully ignorant.

Phil
 
B

BartC

Keith Thompson said:
The second style has *no* availability in the C language. If
lcc-win32 allows it, it's an extension, not part of the language. Not
that there's anything wrong with that.

I see "Antoninus Twink" hasn't changed. To the original poster: he's
advising you to post your C++ questions to comp.lang.c, ignoring the
fact that there's a perfectly good comp.lang.c++ newsgroup. He's
deliberately trying to mislead you.

I replied to AT because it was the link he provided that told me what &
references were about and allowed me to test them. Perhaps I should have
replied direct to the OP.

And I thought that, while & references seem to be a C++ concept, it was
useful to point out that they might be available on at least some C
implementations, obviously as an extension.
 
J

James Kuyper

CBFalconer said:
If it does that means that lcc-win32 doesn't detect the syntax
error.

More precisely, it isn't a syntax error for the language lcc-win32
compiles, even though it is a syntax error in C.
 
A

Antoninus Twink

I see "Antoninus Twink" hasn't changed. He's deliberately trying to
mislead you.

No, I deliberately tried to help him, by pointing him to a page that
answered his question more clearly and more succinctly than I could have
done by composing a reply myself.

The fact that the name C++ occurs in the URL itself, as well as in the
title of the webpage in great big letters, and in nearly every answer of
the page, makes it very clear that the page is about C++, so there is
hardly a danger of anyone being misled.

Why does it upset you when others make helpful replies to OPs?
 
A

Antoninus Twink

*Everybody* has a tendency to upset some people. Mother Teresa upset
some people. So did Ghandi. So what?

Your lack of self awareness is simply staggering.

In one breath, you compare yourself to Mother Teresa and Gandhi, and in
the next you wonder how anyone could ever consider you to be arrogant.
 
A

Antoninus Twink

And I thought that, while & references seem to be a C++ concept, it
was useful to point out that they might be available on at least some
C implementations, obviously as an extension.

I think it's also useful to have a broader perspective on this sort of
thing, because it helps in understanding some of the conceptual problems
people sometimes have with C.

Parameter passing in C is beautifully simple: everything is passed by
value, and if you need to do what other languages call "pass by
reference", you just pass a pointer and modify the object through that.

In languages like Java, the situation is a mess. Pretty well everything
is passed as a reference, except certain "built in types" like int and
double, which are usually passed by value. But at least you can learn,
for each type, how it will be passed.

C++ is even worse, because if you see code like

int foo = 42;
bar(foo);
assert(foo == 42);

then you have no way of knowing whether the assert will succeed without
looking at the prototype of bar() to find out whether it just takes an
int or a reference to an int. In my opinion, this is horribly confusing,
especially when bar() is some sort of overloaded method in a multiply
inherited class or whatever, and it's hard to find out what the hell is
actually going on.

On the other hand, passing things by constant reference in the public
interface of a class is such a well-established idiom in C++ that it's
hard to avoid running across it.

So it's perfectly natural for people to wonder what the differences in
argument passing are in C compared to other languages, and why shouldn't
they ask about it here? If they want to crosspost to another group, then
that's fine too.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top