Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator overloading Conversions friend inline

Similar presentations


Presentation on theme: "Operator overloading Conversions friend inline"— Presentation transcript:

1 Operator overloading Conversions friend inline
.

2 Operator Overloading Operators like +, - , * , are actually methods,
and can be overloaded. Syntactic sugar.

3 What is it good for - 1 Natural usage. compare: a.set( add(b,c) ) to
a= b+c v.elementAt(i)= 3 v[i]= 3

4 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: Copy constructor Destructor Operator = Or in other words: when you need one, you need all.

5 What is it good for - 3 a and b can be primitives or
Uniformity with base types (important for templates) template<typename T> const T& min(const T& a, const T& b) { return a<b ? a : b; } a and b can be primitives or user defined objects that have operator <

6 Rules Don't overload operators with non-standard behavior! (<< for adding,...) Check how operators work on primitives or in the standard library and give the same behavior in your class.

7 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.

8 Prototype X& operator=(const X& rval) parameter for object on right
side of operator return type method name

9 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;

10 A skeleton for deep copy
// Copy constructor A (const A& other) : init { copy_other(other); } // Operator = A& operator=(const A& other) { if (this!=&other) { // preventing problems in a=a clear(); init // or recycle } return *this; } // allows a= b= c= … // Destructor ~A() { clear(); }

11 IntBuffer example

12 C++-11 Move ctor and assignment
// Move constructor A (const A&& other) { ? } // Move operator = A& operator=(const A&& other) { references-and-move-semantics-in-c++11.html

13 List & Complex examples

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 return cpy; A flag that makes it postfix 14

15 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 return cpy; // For HNum, it might be a good idea not to // implement postfix A flag that makes it postfix 15

16 Conversions of types is done in two cases:
Explicit casting (we'll learn more about it in next lessons) 16

17 Conversions of types is done in two cases:
Explicit casting (we'll learn more about it in next lessons) 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 17

18 Conversion example (conv.cpp)
18

19 Conversions danger: unexpected behavior
Buffer(size_t length) // ctor void foo(const Buffer& v) // function ... foo(3); // Equivalent to: foo(Buffer(3)) // Did the user really wanted this? The Buffer and the size_t objects are not logically the same objects! 19

20 Conversion example (conv_explicit.cpp)
20

21 User defined conversion
class Fraction { ... // double --> Fraction conversion Fraction (const double& d) { } // Fraction --> double conversion operator double() const { 21

22 friend 22

23 friend functions 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 23

24 friend functions example: Complex revisited
24

25 friend classes A class can allow other classes to access its
private data members The friendship is one sided 25

26 friend classes - example
class IntTree { friend class IntTreeIterator; }; // TreeIterator can access Tree's data members IntTreeIterator& IntTreeIterator::operator++() { ... return *this; } 26

27 Google test (not for your test)
FRIEND_TEST(TestCaseName, TestName); Declares that this test will be able to test private methods of the class in which you write this 27

28 Inline functions / methods
28

29 Inline functions / methods
A hint to a compiler to put function’s code inline, rather than perform a regular function call. When the compiler must produce an address of the function, it will always reject our request. Objective: improve performance of small, frequently used functions. An inline function defined in .cpp file is not recognized in other source files. 29

30 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 30

31 Inline methods 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; } }; 31

32 Inline methods 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; } 32

33 Tradeoffs: Inline vs. Regular Functions / Methods
Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. 33

34 Tradeoffs: Inline vs. Regular Functions / Methods
Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. Inline functions – no function call overhead, hence usually faster execution (especially!) as the compiler will be able to optimize through the call ("procedural integration"). 34

35 Tradeoffs: Inline vs. Regular Functions / Methods
Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. Inline functions – no function call overhead, hence usually faster execution (especially!) as the compiler will be able to optimize through the call ("procedural integration"). Inline functions - code is copied into program in place of call – can enlarge executable program 35

36 Tradeoffs: Inline vs. Regular Functions / Methods
Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. Inline functions – no function call overhead, hence usually faster execution (especially!) as the compiler will be able to optimize through the call ("procedural integration"). Inline functions - code is copied into program in place of call – can enlarge executable program Inline functions - can enlarge compile time. You compile the inline function again and again in every place it's used. 36

37 Tradeoffs: Inline vs. Regular Functions / Methods
Inline functions - less information hiding

38 Precompiled Headers May save some compiling time Not in this course

39 Link Time Optimization
Compilers might do inlining even for compiled functions (that were in .cpp files) Not in this course, see discussion here: optimization-and-inline

40 Inline Constructors and Destructors
Constructors and Destructors may have hidden activities inside them since the class can contain sub-objects whose constructors and destructors must be called. You should consider its efficiency before making them inline.


Download ppt "Operator overloading Conversions friend inline"

Similar presentations


Ads by Google