1 / 6

Overloading operators

Overloading operators. Why overload operators? (==, =, <, >>, +=, …) notational convenience match user expectations because we can (except :: and . And .*) C++ heuristic: avoid behind-the-scenes conversion Cpstring v. standard string string filename=“sesame.txt”; ifstream input;

jamip
Download Presentation

Overloading operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overloading operators • Why overload operators? (==, =, <, >>, +=, …) • notational convenience • match user expectations • because we can (except :: and . And .*) • C++ heuristic: avoid behind-the-scenes conversion • Cpstring v. standard string string filename=“sesame.txt”; ifstream input; input.open(filename); // CPstring input.open(filename.c_str()); // standard string • vectors Vector<int> a(20); a[0] = 1; a = 2; // avoid with explicit keyword • www.cs.duke.edu/csed/cplus and Stroustrup Chapter 11

  2. motivation for Membersort<...> class Alphasort : public Compare { public: virtual int operator()(const DirEntry & lhs, const DirEntry & rhs) { if (lhs.name() == rhs.name()) return 0; else if (lhs.name() < rhs.name()) return -1; else return 1; } } class Sizesort : public Compare { public: virtual int operator()(const DirEntry & lhs, const DirEntry & rhs) { if (lhs.size() == rhs.size()) return 0; else if (lhs.size() < rhs.size()) return -1; else return 1; } } • what about sorting by time? DirEntry::time()

  3. Factor out common code, why use a template? class Alphasort : public Compare { public: virtual int operator()(const DirEntry & lhs, const DirEntry & rhs) { if (lhs.name() == rhs.name()) return 0; else if (lhs.name() < rhs.name()) return -1; else return 1; } } template <class Kind> class MemberSort : public Compare { public: typedef Kind (DirEntry::* MemPtr)() const; MemberSort(MemPtr ptr) : myPtr(ptr){} virtual int operator()(const DirEntry & lhs, const DirEntry & rhs) { if ((lhs.*myPtr)() == (rhs.*myPtr)()) return 0;

  4. Alternatives to makeIterator() • return a vector Scandir scanner; Vector<DirEntry> vde; .... scanner.get(vde); • return a C-style array Scandir scanner; DirEntry * varray = scanner.get(); • other options? • advantages/disadvantages compared to iterator? • problems with iterator, why IterProxy? • where is the concrete iterator implemented, why? • is friendship required?

  5. IterProxy • Proxy aka surrogate (GOF 207) • need: more versatile or sophisticated reference to an object than a simple pointer • smart pointer, virtual proxy/web proxy PointerProxy<Iterator<DirEntry> > iter(scanner.makeIterator()); for( iter->first(); ! iter.isDone(); iter->next() )... • note: iter is allocated on the stack, not on the heap • cleans itself up with destructor • acts like a pointer, overload operator-> • potential problem with assignment of proxies

  6. Assignment problems IterProxy<DirEntry> iter(scanner.makeIterator()); IterProxy<DirEntry> copy = iter; .... // both go out of scope • options available to stop double deletion • disable assignment, copy --- how? • transfer ownership on assign or copy, but what to leave behind? • other proxy uses include lazy/on-demand creation, copy on write, e.g., reference counted strings

More Related