Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

pointers or references?

Name: Anonymous 2010-03-08 15:05

Hello /prog/.

Can you help me? I'd like to know the real pros and cons of using pointers instead of references (or vice versa) in C++.

Name: Anonymous 2010-03-08 15:15

SON OF A BITCH SEPPLES
I'M HASKELL
SEPPLES IS PIG
DO YOU WANT SIDE EFFECT?
DO YOU WANT SEGFAULT?
POINTERS ARE PIG DUSGUSTING
STROUSTRUP IS A MURDERER
FUCKING STL

Name: Anonymous 2010-03-08 15:28

References are pointers with better syntax and more safety because you can't change what they point to. Performance-wise they should be equivalent.

Name: Anonymous 2010-03-08 15:29

POINT MY ANUS

Name: Anonymous 2010-03-08 15:30

>>3
Ok, thanks. I don't have much experience with C++, but until now I've got some bad experience about it.

I'd like to know what you would recommend between using pointers and references.

Name: 3 2010-03-08 16:13

>>5
I don't have much experience with C++, but until now I've got some bad experience about it.
All experience with C++ is bad experience.

I'd like to know what you would recommend between using pointers and references.
Use pointers. Drop the ++ silliness and stick to ANSI C for your low-level needs to minimize your future headaches.

Name: Anonymous 2010-03-08 16:38

>>3
const

Name: Anonymous 2010-03-08 21:48

Use references when all you intend to do is read or change the state of a given object. Use pointers when you actually need to be dealing with memory addresses, or in case when you might intentionally expect or handle a NULL value.

In most cases, references do the job you need. As an added bonus, (in my opinion) they're also much cleaner to read and safer to write. The act of dereferencing a pointer may give the appearance of being more explicit about intent, but it also adds syntax clutter (ref_one = ref_two * ref_three; vs. (*ref_one) = (*ref_two) * (*ref_three);[/code] -- and, as a result, more chances for error.

Name: Anonymous 2010-03-08 21:49

OK, in all fairness, my bbcode fail makes the pointers look worse.

ref_one = ref_two * ref_three;
(*ptr_one) = (*ptr_two) * (*ptr_three);

Name: Anonymous 2010-03-09 1:26

>>9
**& ptr_one =* ptr_two */*/*/* ptr_three;

Name: Anonymous 2010-03-09 5:04

>>9,10
Why are ptr_one and ptr_three defined as ptr_two types?

Name: Anonymous 2010-03-09 10:21

>>9
(*ptr_one) = (*ptr_two) * (*ptr_three);

Perhaps you should look at the operator precedence table.

Name: Anonymous 2010-03-09 11:37

>>12
Sure, you could rely on knowing the order and take out the parens and thereby, perhaps, introduce some ambiguity depending on how complex the operation is.

*p = **p++ + ++p + (*q)++; // fuck, i dunno, something like this maybe

Or maybe not; perhaps you're the only one working on the project, or you're working on a team of 10 yous.

But that's all beside the point, because no matter what you do, the solution using references will be easier to read and safer to write. It's just like the original variable it references. In fact, it is the original variable it references. If that's all you need (and often, it is!), then references are a great choice.


Note: For what it's worth, at the end of the day, the assembly is the same for both a reference and a pointer anyway, as far as I know.

Name: Anonymous 2010-03-09 11:39

>>13
I definitely could have made that example more complicated. That's pretty tame, in fact.

Name: Anonymous 2010-11-26 12:32

Name: Anonymous 2011-02-02 22:33


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List