Re: C to Java migration - how to cope with passing by value only?
From: Woebegone (woebegoneDELETE_at_THIScogeco.ca)
Date: 01/20/05
- Next message: kjc: "Re: Is EJB suitable for this...?"
- Previous message: Dobieslaw Wroblewski: "Re: C to Java migration - how to cope with passing by value only?"
- In reply to: Dobieslaw Wroblewski: "C to Java migration - how to cope with passing by value only?"
- Next in thread: Dobieslaw Wroblewski: "Re: C to Java migration - how to cope with passing by value only?"
- Reply: Dobieslaw Wroblewski: "Re: C to Java migration - how to cope with passing by value only?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 19 Jan 2005 21:38:09 -0500
"Dobieslaw Wroblewski" <dobieslaw.wroblewski@reverse_the_next.com.cegedim>
wrote in message news:csmt3r$q7e$1@nemesis.news.tpi.pl...
> Hi,
>
> Yesterday I tried to write something like (it's not the exact code):
>
> void Foo(Color cl, JButton bn) {
> cl = new Color(200, 200, 200);
> bn = new JButton("Button");
> bn.setbackground(cl);
> }
>
> And to my amazement - it did not work as I expected. Then I read in the
> tutorial that the references to classes are passed by value in Java...
> because everything is passed by value in fact ;-).
>
> I was previously programming mainly in C++ and used passing arguments by
> reference or by pointer quite often. Are there any well established
> strategies of how to change the way of thinking (this is probably easier
> ;-)
> and how to port the C++ code so that it uses passing by value only? I can
> think of passing Object[] to functions like Foo, but this does not seem
> very
> elegant...
>
> DW.
>
>
Think of the Java method
void foo(A a) {
a = new A();
}
as working like the C method
void foo(A* a) {
a = new A;
}
Neither change the argument itself: just the copy of it they receive through
the parameter, which is not the Object either -- what we're interested in.
In C you have to write
void foo(A** a) {
*a = new A();
}
In C++, with reference parameters, I suspect you're not really doing quite
what you think you're doing either: bear in mind that it's impossible to
reseat a reference, and that a reference can never be null (even though it
can refer to a null "object"), so you can modify the thing a reference
points to but not the reference itself.
If I have the C++
void (A& a) {
a = new A;
}
it's more like the second example than the first.
In your example, you're trying to adjust what the reference refers to rather
than the thing that it refers to, and that can't be done. Like you
considered, you can synthesize another layer of indirection by wrapping your
references in another object, and using that as the parameter:
// Data hiding omitted for brevity...
public class ColorButton {
public JButton button;
public Color color;
}
void foo(ColorButton cb) {
// this is going to throw a null pointer exception if
// cb is null
cb.color = new Color(200, 200, 200);
cb.button = new JButton("Button");
cb.button.setBackground(cb.color);
}
Alternatively, why not omit the arguments and make the button your return
value? You can query it for it's colour (I mean Color) later if you need to
know:
public JButton foo() {
JButton button = new JButton("Button");
button.setBackground(new Color(200, 200, 200));
}
As for the other question, how to get used to it, I think the only way is to
get burned a few times (or many times for some us...) and get used to
consciously avoiding side-effects in functions ( I mean methods).
-- Good Luck! Sean.
- Next message: kjc: "Re: Is EJB suitable for this...?"
- Previous message: Dobieslaw Wroblewski: "Re: C to Java migration - how to cope with passing by value only?"
- In reply to: Dobieslaw Wroblewski: "C to Java migration - how to cope with passing by value only?"
- Next in thread: Dobieslaw Wroblewski: "Re: C to Java migration - how to cope with passing by value only?"
- Reply: Dobieslaw Wroblewski: "Re: C to Java migration - how to cope with passing by value only?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]