operator overloads part2. issue provide member +(int) operator rational + int ok int + rational...

38
Operator Overloads Part2

Upload: lewis-george

Post on 02-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Operator Overloads Part2

Issue

• Provide member+(int) operator

• Rational + int OK• int + Rational Error

Compiler View

• What compiler sees

r2 + 4r2.operator+(4) call + function on r2, pass it 4

4 + r24.operator+(r2) call + function on 4?!?!?

Overload Options

• Overload operator as member:• r1 + r2 interpreted as r1.operator+(r2)

• Overload operator as non-member:• r1 + r2 interpreted as +(r1, r2)

Overload Option 2

• Provide operator as non-member function• r1 + r2 interpreted as +(r1, r2)

NOT a member. No Rational::

Option 2 Implementation

• Standard function• r1 + r2 interpreted as +(r1, r2)

– Need two parameters

– No private access

Option 2 Advantage

• Standard function operator can place class last:

• Compiler view:

4 + r2

+(4, r2) Call + function with 4 and r2

Order matters

• 4 + r2 not the same as r2 + 4

Which Option?

• Implement operators as members or functions?

• No perfect answer…– Members : • Access to private data• First operand is implicit – must be your type

– Standard function• First operand can be other type• Normally don't have access to privates…

Friends With Benefits

• Friend's of a class have access to its privates:

OK if B is A's friend

Private access not allowed

Friend +

• Can declare functions as friends:–Declares function–Makes friend of class

–Givesprivateaccess:

Other Overloads

• Relational as non-members:

Relational Implementation

• Compare first to second, return true/false

Nonmember implementation

Type Conversions

Type Conversions• One arg constructors used for casts and

implicit conversions:

+ int without operator

• With non-member function, compiler will implicitly construct objects to make operators work:

Type Conversions• explicit keyword on constructors prevents

implicit conversions

Type Conversions

• Conversion operators define how to cast away from Rational – Written as member function– Return type used as operator name

Conversions at Work:

• Any situation requiringprimitive, compilerwill use conversion operator:

Ambiguous Conversions

• Too many choices confuses compiler:

• Make things explicit:

[ ], << and >>

LValues and RValues

• LValues : Things that can be on left side of =– Variables, other storage

• RValues : Everything else– Temporary values

BAD Subscript

• Can overload subscript operator– Easy access to member data

???

2 = 3

r1

numerator 2

denominator 5

BAD Subscript

• Subscript supposed to produce Lvalue– returned int is not an Lvalue

GOOD Subscript

• Subscript must return a reference

r1.numerator = 3

r1

numerator 2

denominator 5

IO Operators

• Most classes should not do direct IO– No knowledge of context they will be used in

• toString() function better but clunky

IO Operators

• Most classes should not do direct IO– No knowledge of context they will be used in

• toString() function better but clunky

• Want to directly output/input:

IO Overloads

• Stream always leftoperand of insertion /extraction operators:Must overload << >> as friends

IO Overloads

• Overloading << >>

– ostream means any output stream• File, cout, stringstream…

– istream any input stream– Return reference to the stream to allow chaining

IO Overloads

• Overloading << >>

– ostream means any output stream• File, cout, stringstream…

– istream any input stream– Return reference to the stream to allow chaining

cout

Output

• Stream insertion:– Pick info to send to stream– Return the stream (as reference)– Friend, so has private access

Input• Stream extraction:– Read what you want from stream

• May discard some

– Modify object passed as reference– Return the stream (as reference)

Input• Stream extraction process:

12/30

rational

numerator ?

denominator ?

Input• Stream extraction process:

12/30

rational

numerator 12

denominator ?

Input• Stream extraction process:

12/30

rational

numerator 12

denominator ?

Input• Stream extraction process:

12/30

rational

numerator 12

denominator 30