next up previous contents
Next: General code structure Up: Some basics of object-oriented Previous: References (``&'') in subroutine   Contents

``.'' vs. ``->''

In the above, methods inside classes are addressed via the -> operator. Sometimes, one has to use the . operator instead. Unfortunately, we are unable to write efficient code which uses consistently one or the other, so you need to understand the difference. That difference is that x->y() means that x is a pointer, while x.y() means that x is the object itself or a reference to it. As a rule of thumb, we will use ``->'' when we use objects, and ``.'' when we use containers. For example:


{}
typedef map<Id,Node*> Nodes ; // Nodes contains *pointers* to Node!
Nodes nodes ; // nodes is *not* a pointer
...
for (Nodes::iterator nn=nodes.begin() ; // since ``nodes'' is not a
                     nn!=nodes.end()    // pointer, ``.'' is used.
                     ++nn ) {
Node* node = nn->second ; // ``node'' is now a pointer
...
cout << node->id() << endl ; // ``->'' is used
...



2004-02-02