6.4 Structures Operations


Objectives:

At the end of this lesson you will be able to:

Use operations with structure members

Assume the following declaration



6.4 Structures Operations


Assignment

A struct variable can be assigned to/from.

Comparison - NO

The comparison operators do not work on structs. To compare structs, compare individual fields.

It is not possible to write p1== p2.

There are good reasons to forbid comparison.

What would a greater than comparison even mean on a Point for example.
A bit-by-bit equal comparison is not feasible in general because there may be padding or unfilled elements (eg in a C-string).


6.4 Structures Operations


Arithmetic operators - NO

By default none of the arithmetic operators work on structs.

I/O - NO

The I/O operators >> and << do not work for structs; you must read/write the fields individually.

Solutions

You may redefine operators so that they do work with your structs. When providing functions and overloaded operators for your struct, use the class keyword instead -- it's what programmers expect.