VERSIONARY::BLOG






Recent Entries
firefox os x tricks
Let your Struts DispatchActions Fall through
thoughts on tapestry
#1
Netgear WGR614v2 stinks!
10 years? oh my
Riding through Chernobyl
Thoughts on Class Naming
One Tabbed Browser to Rule them All
The Fog of War


Monthly Entries
October 2004
August 2004
July 2004
June 2004
April 2004
March 2004
November 2003
October 2003
September 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003


Links n' Stuff

Wil Wheaton

JGuru
IntelliJ Tech Net
Oracle Tech Net
The Serverside
Javaranch Forums
JavaLobby

Coast to Coast
Terry Tate: My Hero



Categories
everything else
java
 


March 18, 2003

hrm, c++

Ok, so i'm kind of doing this backwards, I've been programming java for about 3 years, and before that I was a solaris sys admin the only programming I really did being perl and shell with some very light c thrown in for good measure. I fundamentally understood C/C++ and I was always interested in programming, I just never could pick up c/c++ and get into it completely and since java came along I never really looked back. But for a lark I decided to really dig into c++ again to see if i could actually get it this time, I love java dearly, but I always kinda wanted to get a firm handle on c++, but over the years of casual interest/study I never really felt competent.

I was browsing at a bookstore last week and saw Ivor Horton's beginning visual c++ started leafing through it, and said wow, he's actually making sense to me and explaining things the way I always wanted them explained.

So the next day I ordered it on bookpool. Java always made sense to me, but whenever I looked at c++ it just looked ugly and weird, this book has helped me understand a lot better the ugly weird stuff.

So far it's been a pretty fun endeavor, there are some really interesting bits in c++ that are not in java, there are also a bunch of things I never had to do or think about before. One interesting new idea that I never really came across before was pointers to methods. More interestingly passing as part of a method signature a pointer to a method, That blew my mind a little. I'm not sure when and why you'd want that for an app, maybe some odd twist on the Command pattern.

Anyway here comes the newbie question I have on my mind at the moment to any of you c++ types out there. Pass by reference or passing a pointer, is one more preferable than the other in a certain situation.

for example:
void method(int&); versus void method(int*);

Is there an advantage or reason to use pass by reference other than the fact you don't have to dereference it? I noticed that pass by reference is used in an example about writing proper copy constructors but the reason is not apparent to me at this time.

Posted by Andre Mermegas at March 18, 2003 12:07 AM | TrackBack
Comments

Points to methods and functions are useful for doing plugin types of things. Where in Java we can do a Class.forName(...) or ClassLoader.loadClass(...), dynamic linking in C/C++ is more difficult. Though, dynamic libs can have entry points and can export function/method pointers which you can then call, not really caring where they came from.

Posted by: bob mcwhirter on March 18, 2003 12:30 PM

A reference can only be bound upon initialization and cannot change what it's referencing. A point can point at different things throughout its lifespace. There's also issues with data-slicing, I think? It's been soooo long.

Now, you can have lots of fun with pointers to pointers.

Posted by: bob mcwhirter on March 18, 2003 12:40 PM

Ahh interesting, thanks for sharing some insight.

The part about a reference being const and a pointer unless explicitly declared const can be reassigned i knew, but forgot to mention.

Posted by: Andre Mermegas on March 18, 2003 02:09 PM

As far as I can remember, in most C++ implementations a reference is handled internally the same way like a const pointer.

This is IMHO one of the C++ features that makes the language more complicated, without buying you very much.

I recommend you to read the more effective C++ books written by scott meyers. He explains these things in great detail.

Posted by: Markus Kohler on March 19, 2003 04:27 AM
Post a comment



















October 2004
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31