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