programming languages - design principles,
implementation constructs
c++
-
abstraction mechanisms: classes and operator overloading
-
class/object construction - operator overloading
-
binary and unary operators -
-
An operator function intended to accept a basic type as
its first operand cannot be a member function.
-
For example, consider adding a complex variable
aa
to the integer 2; aa+2
can,
with a suitably declared member function, be interpreted
as aa.operator+(2)
, but 2+aa
cannot because there is no class int
for which to define +
to mean
2.operator+(aa)
.
-
Even if there were, two different member functions
would be needed to cope with
2+aa
and
aa+2
. Because the compiler does not know
the meaning of a user-defined +
,
it cannot assume that it is commutative and interpret
2+aa
as aa+2
. This example is
trivially handled by using nonmember functions.
<<
|
1 |
basic facilities
<<
3
>>
class derivation
| 5
| 6
| 7
| 8