DOC PREVIEW
UW CSE 303 - Lecture Notes

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsAutumn 2008Lecture 25— C++ Overriding and Wrap-up;Manual Memory-Management IdiomsCSE303 Autumn 2008, Lecture 25 1'&$%Method overriding, part 1If a derived class defines a method with the same name and argumenttypes as one defined in the base class (perhaps because of anancestor), it ove rrides (i.e., replaces) rather than extends.If you want to use the base-class code, you specify the base class whenmaking a method call.• Like super in Java (no such keyword in C++ since there may bemultiple inheritance)Warning: the title of this slide is part 1.CSE303 Autumn 2008, Lecture 25 2'&$%Casting and subtypingAn object of a derived class cannot be cast to an object of a base class.• For the same reason a struct T1 { int x, y, z; } cannot becast to type struct T2 { int x, y; } (different size)A pointer to an object of a derived class can be cast to a pointer to anobject of a base class.• For the same reason a struct T1 * can be cast to typestruct T2 * (point to a prefix of the memory)• (Story not so simple with multiple inheritance)After such an upcast, field-access works fine (prefix), but what domethod calls mean in the presence of overriding...CSE303 Autumn 2008, Lecture 25 3'&$%An important exampleclass A {public:void m1() { cout << "a1"; }virtual void m2() { cout << "a2"; }};class B : public A {void m1() { cout << "b1"; }void m2() { cout << "b2"; }};void f() {A* x = new B();x->m1();x->m2();}CSE303 Autumn 2008, Lecture 25 4'&$%In words• A non-virtual method-call is resolved using the (compile-time)type of the receiver expression.• A virtual method-call is resolved using the (run-time) class of thereceiver object (what the expression evaluates to).– Like in Java– Called “dynamic dispatch”• A method-call is virtual if the method called is marked virtual oroverrides a virtual method.– So “one virtual” somewhere up the base-class chain is enough,but it’s probably better style to re peat it.CSE303 Autumn 2008, Lecture 25 5'&$%More on two method-call rulesFor software-engineering, virtual and non-virtual each have advantages(see CSE341):• Non-virtual – can look at the code to know what you’re calling• Virtual – easier to extend code already writtenThe implementations are the same and different:• Same: Methods just be come functions with one extra argumentthis (pointer to receiver).• Different:– Non-virtual: linker can plug in code pointer– Virtual: At run-time, look up code pointer via “secret field” inthe objectCSE303 Autumn 2008, Lecture 25 6'&$%Destructors revisitedclass B : public A { ... }...B * b = new B();A * a = b;delete a;Will B::~B() get called (before A::~A())?Only if A::~A() was declared virtual.• Rule of thumb: Declare destructors virtual; usually what you want.CSE303 Autumn 2008, Lecture 25 7'&$%DowncastsOld news:• C pointer-casts: unchecked; better know what you are doing• Java: checked; may raise ClassCastException(check “secret field”)New news:• C++ has “all the above” (several different kinds of casts)• If you use single-inheritance and know what you are doing, theC-style casts (same pointer, assume more about what is pointedto) should work fine for downcasts.• Worth learning about the differences on your ownCSE303 Autumn 2008, Lecture 25 8'&$%Pure virtual methodsA C++ “pure virtual” method is like a Java “abstract” method.• Some subclass must override because there is no definition in baseclass.• Makes se nse w ith dynamic dispatch.• Unlike Java, no need/way to mark the class specially.• Funny syntax in base class; ove rride as usual:class C {virtual t0 m(t1,t2,...,tn) = 0;...};• Side-comment: with multiple inheritance and pure-virtualmethods, no need for a separate notion of Java-style interfaces.CSE303 Autumn 2008, Lecture 25 9'&$%C++ summary• Lots of new syntax and gotchas, but just a few new concepts:– Objects vs. pointers to objects– Destructors– virtual vs. non-virtual– pass-by-reference• Plus all the stuff we didn’t get to, especially templates,exceptions, and operator overloading.• Maybe later: why objects are better than code-pointers / codingup object-like idioms in CCSE303 Autumn 2008, Lecture 25 10'&$%Memory-management idiomsReview: Java and C mem ory-management rulesIdioms for mem ory-management:• Garbage collection• Unique pointers• Reference Counting (later)• Arenas (a.k.a. regions) (later)Note: Same “problems” with file-handles, network-connections,Java-style ite rators, ...Note: Idioms are not tools, rules, or language-features, rather“common time-tested approaches”• Those are important to learn too.CSE303 Autumn 2008, Lecture 25 11'&$%Java rules• Space for local variables lasts until end of method-call, but noproblem because cannot get pointer into stack• All “objects” are in the he ap; they conce ptually live forever.– Really get reclaimed when they are unreachable (from a stackvariables or global variable).– Static fields are global variables.Consequences:• You rarely think about me mory-management.• You can run out of memory without needing to (e.g., long deadlist in a global), but you still get a safe exception.• No dangling-pointer dereferences!• Extra behind-the-scenes space and time for doing the collection.CSE303 Autumn 2008, Lecture 25 12'&$%C rules• Space for local variables lasts until end of function-call, may leadto dangling pointers into the stack.• Objects into the heap live until free(p) is called, where p pointsto the beginning of the object.• Therefore, unreachable objects can never be reclaimed.• malloc returns NULL if it cannot find space.• If you do the following, HYCSBWK:1. Call free with a stack pointer or middle pointer.2. Call free twice with the same pointer.3. Dereference a pointer to an object that has been freed.• Usually 1–2 screw up the malloc/free library and 3 screws up anapplication when the space is being used for another object.CSE303 Autumn 2008, Lecture 25 13'&$%Garbage Collection for CYes, there are garbage collectors for C (and C++)!http://www.hpl.hp.com/personal/Hans_Boehm/gc/• redefines free to do nothing• unlike a Java GC, conservatively thinks an int might be a pointer.Questions to ask yourself in any application:• Why do I want manual memory management?• Why do I want C?Good (and rare!) answers against GC: Tight control over performance;even short pauses unacceptable; need to


View Full Document

UW CSE 303 - Lecture Notes

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?