Using References with a Structure

References work wonderfully with structures and classes, C++’s user-defined types. Indeed, references were introduced primarily for use with these types, not for use with the basic built-in types.

The method for using a reference to a structure as a function parameter is the same as the method for using a reference to a basic variable: You just use the & reference operator when declaring a structure parameter. For example, suppose we have the following definition of a structure:

struct free_throws{    std::string name;    int made;    int attempts;    float percent;};

Then a function using a reference to this type could be prototyped as follows:

void set_pc(free_throws & ft);   // use a reference to a structure ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.