|
-
April 4th, 2013, 01:00 PM
#7
Re: When to use pointers?
 Originally Posted by royibernthal
1) Do I have any real reason to use pointers over references here? So far I managed to pull off everything I tried with references only, maybe it could have been done more efficiently though using pointers I really have no idea.
One rule of thumb concerning efficiency and C++:
Unless you're writing a function that implements a general algorithm, trying to guess in C++ what code is more efficient than another code will yield you, for the most part, wrong answers.
Too many persons new to C++, or persons who know another language but think they know C++ well enough, almost always guess wrong as to what code is more efficient than another piece of code. One main reason is that the C++ code we see is usually not the same code after the compiler finishes its optimization process. Yes, the code will execute properly, but the compiler has done "tricks" to make it more optimal than what the original source code suggests.
Second, as far as using a pointer, maybe the function wants to accept NULL as an argument. You can't legally pass NULL references in C++, but you can pass NULL pointers.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 4th, 2013 at 01:04 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|