spacer.gif spacer.gif spacer.gif spacer.gif spacer.gif spacer.gif spacer.gif spacer.gif spacer.gif
    HeaderTop2.gif
HeaderTop1.gif spacer.gifShareware, Training and Consultancy in software

Focus
Software
Solutions

Site Contents
Home
Links
Enquiries Contact
Services
IT Training
Consultancy
Bespoke Software
Products
ImageDB
SyncWeb
SysSync
Free Downloads
ImageDB
SyncWeb
SysSync
Support
Forums & FAQ
Contact
Personal
Photo Gallery
Peru 03
Demo page from the C++ course Return...
Containers as Class Members

It is very common to have a container as a class member and iterators may be used internally when accessing this container as in the portfolio::display function shown above.

There is a problem though logically the display function should really have been declared as a const member function; it should not be modifying the state of the container. However the iterator could be used to modify the data:

*iter = ...;

In order to declare the function const the compiler needs to know that the iterator is safe will not and cannot be used to modify the contents of the container. This problem can not be solved by declaring the iterator constant const list<Transaction>::iterator as this would mean the iterator could not change value, rather than the data iterated to. In other words it would not be possible to increment the iterator to move it through the container. This would not be very useful and a different kind of iterator is required that cannot change the state of the element iterated to.

Note the use of the -> operator in the code above. This operator has also been overloaded so if the contained data is a structure or class, the iterator can still be used just like a pointer using the normal -> operator notation.

© Focus Software Solutions Limited 2005