Learn how a functor is implemented as a class that overloads the function call operator. This is useful for carrying persistent data along with a function.
- [Instructor] A functor is simply a class with…an operator overload for the function called operator.…We've talked about this elsewhere,…so here's a quick overview for the purposes of this chapter.…Here I have a working copy of functor.cpp…from chapter five of the exercise files.…You'll notice this class definition here,…it's a template class with a template type of T.…It keeps a multiplier,…and you can set or get the multiplier.…There's a constructor that takes the multiplier,…and then there's a functor, which is this…operator overload for the function called operator,…which returns the multiplier times the operad…passed to the functor.…
So very simple, very straight forward.…But it gets to carry forward information,…carry forward data by virtue of the fact…that it's a class and not a function.…So here we have an instance of this class…of type int, it's called X…we construct it with a nine for the multiplier…and then we change it later on to a seven multiplier,…and when I build and run…you can see we get multiples of nine,…
- Template fundamentals
- STL containers
- Accessing iterators
- Output, forward, and bidirectional iterators
- The transform function
- Transforming strings and types
- Arithmetic, relational, and logical functions
- STL algorithms
Skill Level Intermediate
2. STL Containers
3. STL Iterators
5. STL Functions
6. STL Algorithm
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.