This is a discussion on Pointers? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by brewbuck Originally Posted by nadroj i think it is a subjective issue. Not really. "not really" by ...
dont worry i wont
again, the functions purpose is obvious. a required side effect or postcondition is that the parameter x is even after the function has returned.
anyways, we arent being constructive and this is doing no good to this thread or forum.
i agree (though ive never heard of that design paradigm before!).
from the start, i said it was a silly example, but i just thought of something quick and on the spot to give the thread starter an example. if this were a real-world application, of course the function and postcondition would be documented in detail, so the design team doesnt assume anything incorrectly. it is possible the thread starter isnt even reading this thread anymore, however you or anyone of course can always give them different or better examples to help their understanding.
also, if you have a large object, assigning or passing it by value requires you to make a copy of that object which can be quite resource-intensive. a pointer is the same size as an int.
judicious use of pointers can make your code more efficient, reliable, and reduce the total amount of work you have to do.
There would be a third, though it is related to the first: for the implementation of data structures that involve "links" of some sort, e.g., linked lists and trees. Also, the second is not as important in C++ due to the existence of reference types, even though C++ references are themselves typically implemented as const pointers.Originally Posted by nadroj
Actually, only arrays are "passed by reference" due to them decaying to a pointer to their first element, since the pointers themselves are passed by value.Originally Posted by nadroj
Since we are discussing C++, they are passed by pointer or by address, not reference.
4 good reasons (most already stated) for using pointers (and/or references):
- Dynamic memory (not so common in C++; usually std::vector is used).
- Allow other functions to modify local variables (if they are not passed by pointer/reference, copies are made and thus the original values are not changed).
- Avoid overhead of copying data (only the pointer would be duplicated).
- Allow several places in the program to see the same data and be able to update it and make the changes reflect everywhere this data is used.
Well, this is C++, so pass-by-reference means using references. There I object to the term. I always have and this is even a better reason to object it. Avoids confusion.
We will not have this discussion again.
All the buzzt!
"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
We will not.
This thread is full of hate.
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.