03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.7. ACCESSING OBJECT MEMBERS 81<br />

Of course, the long <strong>for</strong>m is almost never called but it illustrates that operators are regular<br />

functions that only provide an extra syntax to call them.<br />

Free functions allow qualifying the const-ness of each argument. Member functions do not even<br />

mention the processed object in the signature. How const-ness can be specified then? There is<br />

a special notation that notates the applicability of a member function to constant objects after<br />

the function header, e.g. our subscript operator:<br />

class vector<br />

{<br />

public:<br />

const double& operator[](int i) const<br />

{<br />

assert(i >= 0 && i < my size);<br />

return data[i];<br />

}<br />

};<br />

The const attribute is not just a casual gesture of the programmer that he/she does not mind<br />

calling this member function with a constant object. C ++ takes this constancy very seriously<br />

and will verify that the function does not modify the object, i.e. some of its members, that the<br />

object is only passed as const when free functions are called and that called member functions<br />

have the const attribute as well.<br />

This constancy guarantee also impedes returning non-constant pointers or references. One can<br />

return constant pointers or references as well as objects. A returned object does not need to<br />

be constant (but it could) because it is a copy of the object, of one of its member variables<br />

(or constants), or of a temporary variable; and because it is a copy the object is guaranteed to<br />

remain unchanged.<br />

Constant member functions can be called <strong>for</strong> non-constant objects (because C ++ implicitly<br />

converts non-constant references into constant references when necessary). There<strong>for</strong>e, it is<br />

often sufficient to provide only the constant member function. For instance a function that<br />

returns the size of the vector:<br />

class vector<br />

{<br />

public:<br />

int size() const { return my size; }<br />

// int size() { return my size; } // futile<br />

};<br />

The non-constant size function does the same as the constant one and is there<strong>for</strong>e useless.<br />

For our subscript operator we need both the constant and the mutable version. If we only<br />

had the constant member function, we could use it to read the elements of both constant and<br />

mutable vectors but we could not modify the elements. By the way, our abandonned getters<br />

should have been const since they are only used to read values regardless of whether the object<br />

is constant or mutable.<br />

3.7.4 Accessing multi-dimensional arrays<br />

Let us assume that we have a simple matrix class like the following:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!