11.3. Implicit variable access

Actually, the new version of Time::print is more complicated than it needs to be. We don’t really need to create a local variable in order to refer to the instance variables of the current object.

If the function refers to hour, minute, or second, all by themselves with no dot notation, C++ knows that it must be referring to the current object. So we could have written:

void Time::print () {
  cout << hour << ":" << minute << ":" << second << endl;
}

This kind of variable access is called implicit because the name of the object does not appear explicitly. Features like this are one reason member functions are often more concise than nonmember functions.

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

You have attempted 1 of 3 activities on this page