operator overloading conversions friend inline · inline functions / methods • a hint to a...

33
. Operator overloading Conversions friend inline

Upload: others

Post on 22-Jun-2020

4 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

.

Operator overloading

Conversions

friend

inline

Page 2: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

Operator Overloading

Operators like +, - , * , are actually methods,

and can be overloaded.

Syntactic sugar.

Page 3: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

What is it good for - 1

• Natural usage.

• compare:

• a.set( add(b,c) )

• to

• a= b+c

• compare:

• v.elementAt(i)= 3

• to

v[i]= 3

Page 4: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

What is it good for - 2

Semantic integrity.

A rule of thumb:

When you need to make a deep copy of an

object, you need to define all of these:

1. Copy constructor

2. Destructor

3. Operator =

Or in other words:

when you need one, you need all.

Page 5: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

What is it good for - 3

Uniformity with base types (important for templates)

template<typename T>

const T& max(const T& a, const T& b) {

return a<b ? a : b;

}

a and b can be primitives or user defined objects that have operator <

Page 6: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

Rules

1. Don't overload operators with non-standard

behavior! (<< for adding,...)

2. Check how operators work on primitives or in the

standard library and give the same

behavior in your class.

Page 7: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

Example of usage in

primitives/standard library

>> << are used as bit operations for primitives

numbers and for I/O in the standard library

iostreams classes.

[] is used as subscripting primitives arrays and

vector class in the standard library.

() is used for function calls and for functor objects

in the standard library.

Page 8: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

X& operator=(const X& rval)

return type

method name

parameter for object on right

side of operator

Prototype

Page 9: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

Invoking an Overloaded Operator

Operator can be invoked as a member function:

object1.operator=(object2);

It can also be used in more conventional manner:

object1= object2;

Page 10: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

A skeleton for deep copy

// Copy constructor

A (const A& other) {

// init

// copy other

}

// Operator =

A& operator=(const A& other) {

if (this!=&other) { // preventing problems in a=a

// clear

// copy other

} return *this; } // allows a= b= c= …

// Destructor

~A() {

// clear

}

Page 11: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

C++-11 Move ctor and assignment

(not learned in this class) // Move constructor

A (const A&& other) {

?

}

// Move operator =

A& operator=(const A&& other) {

?

}

http://www.cprogramming.com/c++11/rvalue-

references-and-move-semantics-in-c++11.html

Page 12: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

List & Complex examples

Page 13: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

13

Operators ++ -- postfix prefix

// Prefix: ++n

HNum& operator++() {

code that adds one to this HNum

return *this; // return ref to curr

}

// Postfix : n++

const HNum operator++(int) {

Hnum cpy(*this); // calling copy ctor

code that adds one to this HNum

return cpy;

}

A flag that makes it postfix

Page 14: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

14

Operators ++ -- postfix prefix

// Prefix: ++n

HNum& operator++() {

code that adds one to this HNum

return *this; // return ref to curr

}

// Postfix : n++

const HNum operator++(int) {

Hnum cpy(*this); // calling copy ctor

code that adds one to this HNum

return cpy;

}

// For HNum, it might be a good idea not to

// implement postfix

A flag that makes it postfix

Page 15: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

15

Conversions of types is done in

two cases:

1. Explicit casting (we'll learn more about it in next

lessons)

Page 16: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

16

Conversions of types is done in

two cases:

1. Explicit casting (we'll learn more about it in next

lessons)

2. When a function gets X type while it was

expecting to get Y type, and there is a casting

from X to Y:

void foo(Y y)

...

X x;

foo(x); // a conversion from X to Y is done

Page 17: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

17

Conversion example (conv.cpp)

Page 18: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

18

Conversions danger:

unexpected behavior

std::vector(unsigned int length) // ctor

void foo(const std::vector& v) // function

...

foo(3) // Did the user really wanted to send a vector

with 3 cells (all with value of 0) ?

vector and an unsigned int are not logically

the same object !

Page 19: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

19

Conversion example

(conv_explicit.cpp)

Page 20: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

20

User defined conversion

class Fraction {

...

// double --> Fraction conversion

Fraction (const double& d) {

...

}

...

// Fraction --> double conversion

operator double() const {

...

}

...

Page 21: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

21

friend

Page 22: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

22

Friend function in a class:

Not a method of the class

Have access to the class’s private and protected

data members

Defined inside the class scope

Used properly does not break encapsulation

friend functions

Page 23: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

23

friend functions example:

Complex revisited

Page 24: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

24

A class can allow other classes to access its

private data members

The friendship is one sided

friend classes

Page 25: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

25

class IntTree {

friend class IntTreeIterator;

};

// TreeIterator can access Tree's data members

IntTreeIterator& IntTreeIterator::operator++() {

...

return *this;

}

friend classes - example

Page 26: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

26

Inline functions / methods

Page 27: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

27

Inline functions / methods

• A hint to a compiler to put function’s code inline,

rather than perform a regular function call.

• Objective: improve performance of small,

frequently used functions.

• An inline function defined in .cpp file is not

recognized in other source files.

Page 28: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

28

C vs C++ : macro vs inlining

compare:

define SQRT(x) ((x)*(x))

SQRT(i++) // unexpected behavior

to

inline int sqrt(int x) { return x*x; }

sqrt(i++) // good behavior

Page 29: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

29

You can hint to the compiler that a method is inline

in class declaration (inside the { }; block of a class):

class Tree {

...

size_t size() const{ // automatically hints on inline

return _size;

}

};

Inline methods

Page 30: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

30

You can hint to the compiler that a method is inline

after class declaration:

class Tree {

...

size_t size() const;

...

};

inline size_t Tree::size() const { // still in the h file

return _size;

}

Inline methods

Page 31: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

31

Tradeoffs:

Inline vs. Regular Functions / Methods

• Regular functions – when called, compiler stores return

address of call, allocates memory for local variables, etc.

Page 32: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

32

Tradeoffs:

Inline vs. Regular Functions / Methods

• Regular functions – when called, compiler stores return

address of call, allocates memory for local variables, etc.

• Code for an inline function is copied into program

in place of call – can enlarge executable program, but no

function call overhead, hence usually faster execution.

Page 33: Operator overloading Conversions friend inline · Inline functions / methods • A hint to a compiler to put function’s code inline, rather than perform a regular function call

33

Tradeoffs:

Inline vs. Regular Functions / Methods

• Regular functions – when called, compiler stores return

address of call, allocates memory for local variables, etc.

• Code for an inline function is copied into program

in place of call – can enlarge executable program, but no

function call overhead, hence usually faster execution.

• Can enlarge compile time. You compile the inline

function again and again in every place it's used.