ex10 due Wednesday
ex11 due next Monday (not this Friday)
midterm review Thursday
midterm exam Friday 11:30–12:20, here
paper readings: too difficult? too boring? too long? too many unfamiliar terms?
clang-format -style Google -i
// bad input
# include <iostream>
int main()
{ std::cout << "hello world\n"; return 0; }
// bad input
#include <iostream>
int main() {
std::cout << "hello world\n";
return 0;
}
destructor
copy constructor
copy assignment operator
start from vector v0
#include <iostream>
typedef int T;
struct vector {
size_t size;
T *data;
};
int main(void) {
size_t n = 10;
vector v;
v.size = n;
std::cout << v.size << std::endl;
}
what’s the initial value of vector s
?
change struct
to class
?
add constructor (new T[n] with initial_value) & size() & data()
default value
operator overloading []
explicit vs implicit constructor
hide default constructor? add it using default
initialization lists
range-based loop
valgrind
delete vs delete[] - valgrind again
be careful: shallow vs deep copy
explicit vs implicit
be careful again: shallow vs deep copy
vector v0; // default constructor
vector v0a = 10; // constructor (implicit)
vector v0b(10); // constructor (explicit)
vector v1 = v0; // copy constructor (implicit)
vector v2(v0); // copy constructor (explicit)
vector v3;
v3 = v0; // copy assignment operator
if you define one of
you probably should define all three of them.
reason: the compiler-generated version won’t work.
vector foo(size_t n) {
vector v(n);
return v;
}
vector new_v(v);
return vector
from a function?
copy constructor is heavy-weight
return a reference? lifetime problem.
use a reference to hold the value? compile error.
return value optimization (RVO): try -fno-elide-constructors
rule of five: move constructor & move assignment operator
modify your 3D Point class from lec 9 exercise 1
disable the copy constructor and copy assignment operator
attempt to use copy & assign in code, and see what error the compiler generates
write a CopyFrom()
member function, and try using it instead
write a C++ class that:
is given the name of a file as a constructor argument
has a “GetNextWord()” method that returns the next whitespace or newline-separate word from the file as a copy of a “string” object, or an empty string once you hit EOF.
has a destructor that cleans up anything that needs cleaning up
Write a C++ function that:
uses new
to dynamically allocate an array of strings
uses delete[]
to free it
delete
to delete each allocated stringdelete[]
to delete the string pointer arrayC++ object model