Pointers, References and Values

Passing Parameters, Returning Results, and Storing Member Variables with Musings on Good C++ Style

Mike Crawford

Mike Crawford
Consulting Software Engineer
mike@soggywizards.com

Copyright © 2000, 2001, 2002, 2005, 2016 Michael D. Crawford. All Rights Reserved.

There are three main ways you can refer to a data object in C++:

Which is the best one to use in a given situation? It's a complicated choice, really, and depends on how and where the data is being used. It's my own experience as someone who started out my career as a C programmer (with a background in FORTRAN and BASIC) that I did not always make the best choices, but after learning from experience and reading up on the matter I'm able to write better code now.

Contents

Introduction

Effective C++ cover

Effective C++:
50 Specific Ways to Improve Your Programs and Design (2nd Edition)

by Scott Meyers

[ Buy]  

In what follows I don't claim to be particularly original. A lot of this I got from reading other programmers' source code, talking to coworkers, and reading Scott Meyers' excellent book Effective C++ (I also recommend More Effective C++ and Effective STL). Also the chapters on Large Scale C++ Software Design in More C++ Gems is very worth reading, especially regarding how to structure one's header files. (John Lakos, the author of those chapters wrote a book also called Large Scale C++ Software Design which is very good and goes into much more depth.)

It is interesting to note that part of C++'s power and expressiveness, as well as what makes it complicated to program in, is the very choice that the programmer has. In Java, for example, there is only one choice - one has something that is roughly equivalent to pointers that are used with reference semantics and are all allocated from the heap. It is not a bad choice for most situations, but there is no alternative, not even when another choice would be appropriate, say to gain performance.

Even though I prefer to program in C++ for my own work, I usually advise people to start with Java when they first learn to program (sorry Bjarne!). But I do emphasize that they should study more than one programming language, particularly including C++ and assembly code, and I often say that even if you choose to program in one particular language all the time you should know others well enough that you can understand the relative strengths and weaknesses of each.

More Effective C++ cover

More Effective C++
35 New Ways to Improve Your Programs and Designs

by Scott Meyers

[ Buy]

My own personal feeling is that C++ is more of a Real Programmer's Language, while Java is a fun toy best suited for kids and education. Like any powerful and sharp tool, C++ is more difficult to wield, but the effort expended in learning how to use it well pays off dramatically in terms of the results one is able to achieve with it. Java fans like to say that Java avoids many of the problems they feel are inherent in C++, but I feel that in doing so Java creates its own set of problems which in large-scale projects turn out to be more intractable than the difficulties of C++. Details of this inflammatory accusation will be left for a future article though.

More C++ Gems cover

More C++ Gems

by Stanley Lippman (Foreword),
Robert C. Martin (Editor),
Donald G. Firesmith (Series Editor)

[ Buy]  

I started this article as a single HTML file, but it was getting so long that I broke it up into over a dozen files that link to each other using the "Next" and "Previous" links below. I think this serves as a warning that this really is a complicated and detailed topic, and I worry that I will scare away beginners.

In fact, it was my own experience that my first readings of both Effective C++ and the print version of the C++ FAQ mostly went over my head. I wrote one large C++ program (a test tool for MacTCP) at Apple in 1990, then gave up C++ until I came to Live Picture in 1997. (I'd like to point out that I felt that C++ as it stood on the Macintosh in 1990 was not suited for real product development; I changed my mind in 1997, after C++ had evolved considerably.) After programming in Metrowerks PowerPlant for a couple of months, I read the two books but not very much of the advice stuck.

Large Scale C++ Software Design cover

Large Scale C++ Software Design

John Lakos

[ Buy]  

I persisted with my C++ programming though, and also spent some time programming in Java, and then returned to Effective C++ [ A] in the summer of 2000. After having spent so much time programming on real projects, encountering real problems, and having a better grasp, Meyers' advice stuck with me better. One helpful thing I did was to read his Items one at a time, and after reading one, I'd carefully examine the entire source code of the program I was working on and apply his advice to my code. (This fixed a lot of problems.)

I'd like to suggest that if you are a beginner in C++ that you don't try to swallow this document whole. Bookmark it, read it a bit at a time and come back to it from time to time. Definitely follow up by reading C++ books and participating in the Usenet News groups such as comp.lang.c++.moderated, comp.std.c++ and comp.programming as well as the programmer's newsgroups for the platforms you develop on. Also consider subscribing to developer mailing lists that are oriented to learning C++ such as the NeoProgrammer's Collective or the ACCU Mentored Developers.

Don't think you've screwed up if your program doesn't live up to the recommendations I suggest - this is not a standards document, it consists of suggested recommendations (although I am quite confident of them). And don't think you're destined to failure if you decide you do want to follow my recommendations but can't keep it all in your head while you work or can't get your code to work by writing your program as I suggest.

I've been a career computer programmer since 1987, and I've been working in C++ almost full-time since January of 1997. It is only recently that I feel that I've started to really get a grasp on object-oriented programming, software architecture, and C++. (Note - I wrote that in the Fall of 2000. I feel pretty comfortable with all of those now.) At the same time, I've shipped dozens of commercial software products, from little utilities (my first commercial product, Last Resort from Working Software, ran in 8 kb of memory) to operating systems, as part of a development team with hundreds of engineers (MacOS 7.5.2 and 7.5.3 from Apple Computer).

C++ is a large and powerful language, and it takes a long time and a lot of hard work to learn to use it to its full effectiveness. You can make good use of C++ without having a full understanding of it - many people do! But I promise you that the effort required will be rewarded handsomely by the ease with which you will eventually be able to develop products, and the quality, performance and compactness of the products you develop - if you learn to wield this powerful tool with the care and skill it deserves.

Revision History

August 22, 2005:

Implemented new XHTML Strict with Cascading Stylesheets design created by Paperbeagle Web Design and Custom Graphics. I'll be going over it again shortly to update the text.

May 7 2002:

Added W3C "Valid" icons after checking pages with the HTML Validation Service. Fixed typos and grammar errors. Added links to the Association of C and C++ Users Book Reviews Section when I mention books. Added a number of other links too. Expanded on the use of forward declarations. Added new pages on reference counted smart pointers and exceptions. Added more discussion of optimization in the conclusion. Generally expanded on most of the sections.

September 10, 2001:

Published complete draft

October 2000

Published initial, incomplete draft

One Must Not Trifle With Wizards For It Makes Us Soggy And Hard To Light