7] explain in your own words the concept of a handle class · 7] explain in your own words the...

81
Midterm 2 7] Explain in your own words the concept of a handle class and how it’s implemented in C++: What’s wrong with this answer? A handle class is a pointer vith no visible type.

Upload: others

Post on 20-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Midterm 2

7] Explain in your own words the concept of a handle class and how it’s implemented in C++:

What’s wrong with this answer? A handle class is a pointer vith no visible type.

Page 2: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

• A handle class is a class, not a pointer! (Yes, it does contain a pointer …)

• The type of any pointer must be visible – it’s part of the pointer declaration! (Yes, the definition of that type is in another file …)

What’s wrong with this answer? A handle class is a pointer vith no visible type.

Page 3: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Midterm 2, programming problem 1

Page 4: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 5: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Testing …

Page 6: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 7: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Midterm 2, programming problem 2

Page 8: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 9: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 10: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 11: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

This function is leaking a pointer to a local variable!

Page 12: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 13: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Ch. 11: References & the Copy-Constructor

- continued -

Page 14: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Remember returning objects by value from a function

Page 15: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

• The address of B2 is pushed before making the call. • The function copies the returned object at that address

before returning.

B2’s address

Page 16: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

What can go wrong when copying?

Page 17: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Similar to the text program C11:HowMany.cpp

Compile-time or run-time constant?

Why here? Why not initialize inside the class,

as we did in ch.8?

Page 18: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 19: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 20: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Conclusion

• The low-level, bit-by-bit copying doesn’t always work correctly when combined with C++ constructors.

• We call this bitcopy, to distinguish it from copy-construction (next time).

Page 21: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

We stop before the subsection

Copy-construction

To do for next time:

Thoroughly read and understand the part of the section Copy-constructor covered so far.

EOL 27

Page 22: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ

Why is bit-copy sometimes not enough in C++?

Page 23: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 24: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

The compiler’s assumption is that you want to perform this creation using a bitcopy, and in many cases this may work fine, but in HowMany it doesn’t fly because the meaning of initialization goes beyond simply copying.

Another common example occurs if the class contains pointers – what do they point to, and should you copy them or should they be connected to some new piece of memory?

Page 25: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

[…] intervene in this process and prevent the compiler from doing a bitcopy. You do this by defining your own function to be used whenever the compiler needs to make a new object from an existing object: • We’re making a new object, so this function is a

constructor. • The constructor has a single argument: the object

we’re constructing from. • The argument is passed by reference!

Example

Page 26: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

In general: X(X&)

This function is called whenever a new object is created from an existing one.

Page 27: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Extra-credit:

Page 28: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ Write a class CCClass with:

• An integer data member

• A constructor that initializes the data member

• A copy-constructor that simply announces itself to cout.

16.

Page 29: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Is the name needed here?

Page 30: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ Now create:

• a function that passes a CCClass object in by value and does not return anything

• another function that does not take any arguments, creates a local CCClass object, and returns it by value.

16.

Page 31: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 32: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ Call these functions in the main program. 16.

Page 33: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

How many times is the constructor called? How many times the copy- constructor?

Page 34: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 35: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

What if we call the function, but ignore the return value? A: The compiler creates a temporary., so the C.C. is still called twice!

Page 36: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Default copy-constructor

Because the copy-constructor implements pass and return by value, it’s important that the compiler creates one for you in the case of simple structures – effectively, the same thing it does in C. However, all you’ve seen so far is the default primitive behavior: a bitcopy.

When more complex types are involved, the C++ compiler will still automatically create a copy-constructor if you don’t make one. Again, however, a bitcopy doesn’t make sense, because it doesn’t necessarily implement the proper meaning.

Page 37: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Default copy-constructor

Suppose you create a new class composed of objects of several existing classes. This is called, appropriately enough, composition, and it’s one of the ways you can make new classes from existing classes.

Remember composition from Ch.1!

Page 38: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Ch. 1: Introduction to Objects

Page 39: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Reusing the implementation

Composition

Aggregation

Association

MO

RE

GEN

ERA

L

Page 40: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Owner class Component

class

Page 41: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Composition example (not in text)

Page 42: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Ch. 11: References & the Copy-Constructor

- continued -

Page 43: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 44: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Do you remember what this means?

Page 45: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 46: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

? ?

Page 47: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

To create a copy-constructor for a class that uses composition (and inheritance, which is introduced in Chapter 14), the compiler recursively calls the copy-constructors for all the member objects (and base classes, for inheritance).

The process the compiler goes through to synthesize a copy-constructor is called memberwise initialization.

Conclusion

Page 48: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

To do for next time:

• Read the subsections:

– Preventing pass-by-value

– Functions that modify outside objects

• Re-read and grok the entire section Copy-constructor.

• Solve end-of-chapter ex. 17.

EOL 28

Page 49: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ: What happens when the code passes or returns an object by value and

its class does not have a copy-constructor?

Page 50: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

To create a copy-constructor for a class that uses composition (and inheritance, which is introduced in Chapter 14), the compiler recursively calls the copy-constructors for all the member objects (and base classes, for inheritance).

Default copy-constructor

Page 51: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ: What is memberwise initialization

in C++?

Page 52: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

The (recursive) process the compiler goes through to synthesize a copy-constructor is called memberwise initialization.

Page 53: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Preventing pass-by-value

There’s a simple technique for preventing pass-by-value: declare a private copy-constructor.

If the user tries to pass or return the object by value, the compiler will produce an error message because the copy-constructor is private. It can no longer create a default copy-constructor because you’ve explicitly stated that you’re taking over that job.

Page 54: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Preventing pass-by-value

There’s a simple technique for preventing pass-by-value: declare a private copy-constructor.

If the user tries to pass or return the object by value, the compiler will produce an error message because the copy-constructor is private. It can no longer create a default copy-constructor because you’ve explicitly stated that you’re taking over that job.

Page 55: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

“Deleted” functions were introduced in

C++11

Page 56: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed.

Section 8.4.3

Page 57: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ: Does this program

compile?

Page 58: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ: Does this program

compile?

Page 59: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Pointers to members

A pointer is a variable that holds the address of some location. You can change what a pointer selects at runtime, and the destination of the pointer can be either data or a function.

The C++ pointer-to-member follows this same concept, except that what it selects is a location inside a class.

Page 60: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Pointers to members

The dilemma here is that a pointer needs an address, but there is no “address” inside a class; selecting a member of a class means offsetting into that class.

You can’t produce an actual address until you combine that offset with the starting address of a particular object.

Examples

Page 61: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Remember how we access members of classes and structs:

Examples

Page 62: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Remember how we declare and initialize ordinary pointers:

int a = 42; int *aPtr; aPtr = &a;

int a = 42; int *aPtr = &a;

or

Examples

Page 63: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Examples

Page 64: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

How to read this from R

to L …

Page 65: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ

Create a class containing a double and a print( ) function that prints the double. Don’t forget the constructor!

Page 66: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 67: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ

In main( ), create a pointer to the data member.

Page 68: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 69: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ

Create an object of your class and a pointer to that object.

Page 70: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 71: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ

Manipulate the data member using both the object and the pointer to the object.

Page 72: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 73: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Pointers to members are quite limited: they can be assigned only to a specific location inside a class.

We cannot increment or compare them like ordinary pointers.

Page 74: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Pointers to member functions

Remember pointers to ordinary functions:

int (*fp)(float);

double* (*fp) (int, int*);

Page 75: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

This code is not in the text example. It shows how to use

the pointer fp.

Page 76: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ (continued)

In main( ), create a pointer to the function member print().

Page 77: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 78: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

QUIZ (continued)

In main( ), call the function print() using both the function name and the pointer to the function.

Page 79: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association
Page 80: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Individual work:

• Read and understand the program PointerToMemberFunction2.cpp:

– How to use pointers to members inside objects

Page 81: 7] Explain in your own words the concept of a handle class · 7] Explain in your own words the concept of a handle class ... Reusing the implementation Composition Aggregation Association

Homework for ch. 11

Provided as separate handout (also available on our webpage --> agapie.net)

Due Friday, Nov. 13, at the beginning of class.

Please hand in a hard-copy, do not email!

EOL 29