Then I guess you don't "get" them.Not really. Pointers look natural to me. References look unnatural for the most because of their static nature.
Then I guess you don't "get" them.Not really. Pointers look natural to me. References look unnatural for the most because of their static nature.
That's because they're too static. I like dynamic and not static.
All the time when I design something, references turn up useless all the time due to their nature.
If you purposefully think in terms of pointers then obviously the only kinds of solutions which will occur to you are pointer-based. You want something "dynamic" because that's the way you're used to solving problems. That's fine, but it doesn't make references less useful.
In C++ I typically view pointers as ugly and try to avoid them whenever possible. And that DOESN'T mean sacrificing efficiency or writing stupid code. There are GOOD ways to avoid pointers.
Since I usually have methods that take other classes, I don't use by value, and I can't use references because they need to be initialized, so I really don't see references as anything else but useless most of the time.
I'm sacrificing them for my own savvy design with polymorphism and OO programming.
But there are a few points where they might work, but even there, they're mostly useless because the same can be done with pointers and with or without pointers, there's no complexity in those needs.
This is almost like saying data types like float, char, etc. are useless because we can always use double or int respectively.
Not as I see it.
I can just as easily require a T** instead of T*&, only requiring the user to do &obj instead of obj, which I find perfectly acceptable. Although, I may agree that this might also be the only use for references. Some functions might also require a local argument to perform work to (like a buffer). Passing a reference is acceptable, except you'll probably also end up putting that buffer on the heap and to my knowledge, there's no way to tell if an optional argument passed by reference has been passed or not. With pointers, it's easy, because you'll initialize them to NULL by default.
I also used to pass some function with a return type by value due to that I did not want the responsibility to delete those objects. Now, I use pointers, encapsuled with a smart pointer class, that takes care of that bit. Passing back references is useless since it would just reference a local variable.
Explain to me then why I need floats vs doubles any more than you need references vs pointers.
I won't, because that's up to you. My style of programming makes references useless.
Since pointers and references are being discussed, I just thought somebody could clear this up - which of following functions is more appropriate to use in C++? Or maybe there's a "better" C++ way of handling pointers/references passing/returning? Should I avoid returing pointers at all in C++?
Sorry if this post should't belong here.Code:int main() { string modifyMe; modifyString1( modifyMe ); // or string* modifyAlso = modifyString2(); return 0; } int modifyString1( string& str ) { str.assign( "Hello world" ); } string* modifyString2() { string* str = new string( "Hello world" ); return str; }
Actually, to make the to functions more equal I think your pointer version should look like this:
This shows one very nice things about references -- a reference can never be NULL, so if someone gives you a reference there's no need to check it for NULL.Code:void modifyString2( string* str ) { if ( str != NULL ) str->assign( "Hello world" ); }
Another good thing about them is that they are static. If a reference is initialized at the top of a very long function, then you see it used somewhere near the bottom, you know that it is still pointing to the same object that it was initialized with. With a pointer you'd need to carefully check the whole function down the that point to make sure the pointer wasn't re-assigned to point somewhere else, or worse to point to NULL or a deleted object.
So basically, the developer is required to maintain a lot more of the program state in their head when using pointers than they would using references; and on a friday afternoon when you're half asleep from working late all week, your mind could easily be a little low on 'cache' memory and you could overlook a pointer re-assignment.
Make your own topic next time.
For something as simple as assigning a string to something basic, you should consider just assigning it directly. Depending on what you're doing with the object, there are numerous ways of manipulating it. There may not be a "correct" way of doing it. Interesting to note, in your example, you neglected to delete the string you allocated with new. You might have left it out for brevity, however, for real programs, you have to remember to deallocate memory you allocate, or possibly suffer memory leaks. Because of this possibility, you should consider writing code that is not conducive to allowing memory to be leaked.
Depends on what you want to do. The "real" C++ way (like everyone else here suggests ), would be to take a reference to a string.
If you want to return a string (instead of modifying it directly), consider asking if it's worth to pass the object by value. That way you won't have to delete it later. Of course, you can also use a smart pointer class and return that with a pointer that will clean up itself. Up to you.
Doesn't mean the reference is bound to an allocated object or variable, though.This shows one very nice things about references -- a reference can never be NULL, so if someone gives you a reference there's no need to check it for NULL.
Then just declare those pointers const and assign them. But then again, you could never re-assign them again.Another good thing about them is that they are static. If a reference is initialized at the top of a very long function, then you see it used somewhere near the bottom, you know that it is still pointing to the same object that it was initialized with. With a pointer you'd need to carefully check the whole function down the that point to make sure the pointer wasn't re-assigned to point somewhere else, or worse to point to NULL or a deleted object.
As for NULL and things - that's not a pointer's only use. You can point to other object which does not necessarily be deleted. Very handy, for example, when you walk through a linked list. So you don't need those pointers to point to allocated objects which must be deleted. And if they do, I would use a smart pointer.
Not if you use them correctlySo basically, the developer is required to maintain a lot more of the program state in their head when using pointers than they would using references; and on a friday afternoon when you're half asleep from working late all week, your mind could easily be a little low on 'cache' memory and you could overlook a pointer re-assignment.
A reference is ALWAYS bound to an allocated object or variable, except when there is a mistake in their usage in the first place. That is, either you have a reference to an object which has a shorter lifetime than the reference (most prominently, the classic "return reference to local object" error), or you delete memory you still hold a reference to. Both should be considered errors in their usage.
Now you're just being contrary. Yes, const pointers have the same init-only semantics as references (without the lifetime extension semantics, but few people use those anyway), but references are syntactically more convenient.Then just declare those pointers const and assign them. But then again, you could never re-assign them again.
Linked lists are one of those low-level jobs pointers are good for.As for NULL and things - that's not a pointer's only use. You can point to other object which does not necessarily be deleted. Very handy, for example, when you walk through a linked list.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I think "initialised" may be more accurate than "allocated" here, though in the case of objects of class types the object passed has been initialised, so there is no problem anyway.Doesn't mean the reference is bound to an allocated object or variable, though.
Effectively, this is not a comparison between which is better. Rather, this is a comparison between which is appropriate for the situation.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)