T is a type, T * is the type pointer to T.
T * value is used as the address of a value of type T.
& generates pointer values.
v is a variable of type T, then the expression &v
is a value of type T*.
*
follows (or dereferences) a pointer value to return the associated
value.
*.
"hello world" == "hello world" may or may not be true.
<, >, <= and >=)
determine the order between two pointers pointing into the same
value sequence.
The sum of a pointer and an integer may not be a valid pointer.
new and delete.
new T allocates a storage chunk big enough to
hold a value of type T.
*.
p contains a valid value of type T *,
the expression delete p recovers the storage being pointed to by
p.
p is not changed; but it's now
invalid.
p was returned by a
new statement.
int * i = &j;delete i;
struct C {
data * data_ptr;
C() : data_ptr(new data) { }
};
static void t(void) { C c; }
struct C {
data * data_ptr;
C() : data_ptr(new data) { }
~C() { delete data_ptr; }
};
static void t(void) { C c; }
struct C {
data * p;
C() : p(new data) { }
~C() { delete p; }
};
static void t(C & c) { C c2(c); }
t(), c2.p is c.p.
*(c2.p) changes *(c.p).
*(c2.p) deletes *(c.p).
void t(C c) { }
The call t(c) makes a copy of c.
C::C(const C &).
&.
struct C {
data * data_ptr;
C() : data_ptr(new data) { }
C(const C & c)
: data_ptr(data_clone(c.data_ptr)) {}
~C() { delete data_ptr; }
};
static void t(C & c) { C local_c(c); }
struct C {
data * p;
C() : p(new data) { }
C(const C & c)
: p(data_clone(c.p)) {}
~C() { delete p; }
};
static void t(C & c) { C c2; c2 = c; }
static void t(C & c) { C c2; c2 = c; }
the contents of c.p replaces the contents of
c2.p.
c2.p is garbage.
c2.p and c.p are sharing storage.
=.
=.
struct C {
data * data_ptr;
C() : data_ptr(new data) { }
C(const C & c) : data_ptr(data_clone(c.data_ptr)) {}
~C() { delete data_ptr; }
// Bad example.
C & operator = (const C & c) {
delete data_ptr;
data_ptr = data_clone(c.data_ptr);
return *this;
}
};
a = a, and
a = b = c.
a = a self-assignment does occur in code.
C & C::operator = (const C & rhs) {
delete data_ptr;
data_ptr = copy_data(rhs.data_ptr);
return *this;
}
a = a?
a = a, *this = rhs.
data_ptr deletes rhs.data_ptr.
*this is different from rhs.
*this and rhs are different, proceeds as normal.
*this and rhs are the same, be careful.
C & C::operator = (const C & rhs) {
if (this != &rhs) {
delete data_ptr;
data_ptr = copy_data(rhs.data_ptr);
}
return *this;
}
x = y = z = 0.0;
T & operator = (const T & rhs)
T operator = (const T & rhs)
makes a more expensive copy. (Another one!)
*this.
widget w1 = w2; w1 = w2;

| input | <istream> |
| output | <ostream> |
| both | <iostream> |
ostream & put(char c) ostream & write( const char * s, streamsize cnt) ostream & flush()
good(), fail(), bad(), and eof().
fail() tests for the fail or bad states.
clear()ed.
| Don't do this | Do this |
| while true cin >> x if cin.eof() break // whatever |
! tests a stream's fail or bad
state.
!cin is the same as cin.fail().
std::ofstream of(filename) if (!of) // Can't open the file.
! does not test the stream's end-of-stream state.
while (std::cin >> i) {
// whatever
}
>> performs
formatted input from a stream.
istream >> variable
<< performs formatted
output to a stream.
ostream << value
istream >> x >> y ostream << "y = " << y << "\n"
std::cout << i << " cat"
<< i != 1 ? "s" : "" << ".\n";
-?[0-9]+.
<fstream> header for them all.
fs.open(fname) associates file stream fs and
the file with the name fname using the default modes.
.is_open() member predicate determines if a file-stream
association exists.
.close() member function dis-associates a stream from a file.
istringstream.
ostringstream.
stringstream.
<sstream>.
istringstream constructor primes the stream with the string
to use.
std::istringstream iss("hello");
ss.str() member function returns the string associated with the
string stream ss.
sdt::ostringstream oss; oss << i << " " << j; std::string s = oss.str();
[] provides access to array
elements.
sizeof() operator on an array returns the total number of
bytes in the array, not the number of elements.
a[3];, a is just a constant
pointer to the first (zeroth) element.
a[expr] is just a shorthand for
the expression *(a + expr).
