pointers - { d = (day)(d + 1); return d; } Using pointers,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
References are the preferred way of indirectly accessing a variable. They are also a little safer than pointers and, in some cases, are the only way to achieve a particular result such as overloading certain operators. Consider the following: enum day { Sunday, Monday, . .. }; If we define a variable; day x; and we wanted to write a method to overload the ++ operator such that the statement ++x; increments x to the next day, we could write the following:
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { d = (day)(d + 1); return d; } Using pointers, we may think that the following declaration would work: day *operator++(day *d); However, this statement will not even compile because every overloaded operator function must either be a member of a class, or have a parameter of type T, T &, or T const &, where T is a class or enumeration type. So in this particular case, using a reference is the only way to do it....
View Full Document

Ask a homework question - tutors are online