This topic is now archived and is closed to further replies.

Pointers or References

This topic is 5589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Original post by AfTeRmAtH
Is this another n00b thread just to get replies and feel big?

What the hell are you talking about? Since when did asking a question and/or getting replies make someone "feel big"? Don''t be such a muppet.

Anyway, from the C++ Faq Lite:
8.6 When should I use references, and when should I use pointers?

Use references when you can, and pointers when you have to.

References are usually preferred over pointers whenever you don''t need "reseating". This usually means that references are most useful in a class''s public interface. References typically appear on the skin of an object, and pointers on the inside.

The exception to the above is where a function''s parameter or return value needs a "sentinel" reference. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references should always alias objects, not a dereferenced NULL pointer).

Note: Old line C programmers sometimes don''t like references since they provide reference semantics that isn''t explicit in the caller''s code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine.

Be sure to read the FAQ sometime. It has a lot of useful information in it.

Share this post

Link to post
Share on other sites