Reference or pointer, which is better in my case?
Hi, everyone!
Sometimes we use pointer as the parameter type and sometimes
we use reference as the parameter type.
I think using reference is better than using object directly.
Since it can save memory and operation (assignment operator).
But what about pointer V.S. reference? I want to know in which
case should I use pointer and in which case should I use reference.
Here are two samples,
Using reference,
--------
ostream& operator << (ostream&, ObjectType);
--------
Using pointer
--------
MemPoolAddSize (char*, size_t)
--------
I want to know your suggestions about pointer v.s. reference.
What is the general consideration about when using pointer and
when using reference?
Thanks in advance,
George
Extreme stances on the subject...
I have quickly browsed though this thread, and I think that people are taking extreme stances on the subject to drive home their points. However, anyone unfamiliar with this topic in depth should keep in mind that there is usually an exception to every rule.
Quote:
In c++, it is best to use vectors over arrays.
In many cases this is true. However, if your application is very CPU intensive, there are sometimes arrays are better. For example:
(1) A matrix class. Using a pointer with new to define a (protected) array is usually more efficient than defining a vector of vectors.
(2) In an FFT system analysis program I have been dealing with. I have to aquire and analyze real-time about 1MB/sec of data with even slow P1 and P2 systems. Using arrays instead of vectors here helps optimize the performance.
Quote:
The dead giveaway is if 'C' programming was a prerequisite to the C++ course. If 'C' programming was a prerequisite to C++, then that shows where the problem was -- it presumed that you need to know C before learning C++ -- absolute nonsense. As a matter of fact, it makes the job harder in converting 'C' minds into C++ minds, as the FAQ I posted a link to describes.
C should not be a prerequisite for learning c++, but it should probably be a requisite nonetheless. There are still many systems and libraries out there written in C that someday a C++ programmer will run into. If he/she does not have any knowledge of malloc/calloc/free or some of the other C ways of doing difficult things (like using # and ## in macros), then he/she will be handicapped.
Anyway, I know that I have picked on a couple of quotes from some very knowledgable and experienced people (both of whom I respect very much) -- all I am trying to do is make sure that someone less experienced knows that there are usually cases where the "rules of general practice" do not apply. One of our jobs as programmers is to determine if we have one of those cases.
Cheers!
- Kevin
Re: Extreme stances on the subject...
Quote:
Originally posted by KevinHall
However, if your application is very CPU intensive, there are sometimes arrays are better. For example:
(1) A matrix class. Using a pointer with new to define a (protected) array is usually more efficient than defining a vector of vectors.
Hello Kevin,
C++ gurus knows that vector of vectors is the easy way of implementing a matrix, but know full well of the drawbacks of nested vector classes. This is why such things such as the libraries at www.boost.org exist that efficiently handle these situations.
Quote:
(2) In an FFT system analysis program I have been dealing with. I have to aquire and analyze real-time about 1MB/sec of data with even slow P1 and P2 systems. Using arrays instead of vectors here helps optimize the performance.
Internally, a vector is exactly the same as an array (std::vector uses contiguous blocks of memory). So it depends on what you are doing with the vector that counts. If you are doing nothing different with the vector that you would be doing with an array, then there should be no speed (or very negligible speed difference). If you are inserting stuff in the middle or at the beginning of the vector, then you are using vector in a way that arrays cannot be used. To do the same thing with an array, you would basically do the same thing that vector would do -- allocate new , copy, delete old. Caveat: member functions such as reserve() does wonders for vector if you are calling a lot of push_backs(). Did you utilize it in your testing (that is, if you were calling a lot of push_back's)?
Quote:
There are still many systems and libraries out there written in C that someday a C++ programmer will run into. If he/she does not have any knowledge of malloc/calloc/free or some of the other C ways of doing difficult things (like using # and ## in macros), then he/she will be handicapped.
Believe me, a experienced C++ programmer has full knowledge of these functions, since they know where things such as malloc(), calloc(), qsort(), etc. should be avoided :). Look how much code that purports to be C++ contains malloc(), free(), etc. It takes an experienced C++ programmer to point out that "hey, that can't be done" or "here is the problem of doing it...".
Token pasting macros are neither C or C++, they are just part of the preprocessor.
Regards,
Paul McKenzie