CIS 29 - Notes for Tuesday, 1/16

Announcements and Reminders

Maybe You Haven’t Covered This

typedef and using

The keyword, typedef, originally from C, is used to define a type.

C++ 11 introduced the keyword, using to act like typedef.

typeid operator

The typeid operator returns an identifier of a type, a variable or an expression.  The return of the typeid is a class type, called type_info.  You can use the name() member function of the type_info class to display a literal description of the type.

Example 1-8 – typedef, using, typeid

Note different results for different compilers

Binary File I/O

istream member functions

read

Read a specified number of characters from an input stream and stores them in a char array.  The array is not null-terminated.

istream& read (char* s, streamsize  n);

peek

Returns the next character to be read without extracting it from the input stream.

int peek();

seekg

Sets the next read position in the input stream.  Note, as of C++ 11, seekg will clear the EOF bit, if set.

stream& seekg (streampos  pos);
istream& seekg (streamoff  offset, ios_base::seekdir way);

ios_base::seekdir can be one of three constants

Constant   Meaning
beg        Beginning of the input stream
cur        Current position in the input stream
end        End of the input stream

tellg

Returns the next read position in the input stream.

streampos tellg();

Example 3-1 – istream member functions

ostream member functions


write

Write a specified number of characters to an output stream

ostream& write (const char* s, streamsize n);

seekp

Sets the next write position in the output stream.    Note, as of C++11, seekp will work, even if the EOF bit is set, but it will not change the value of the EOF bit.

ostream& seekp (streampos pos);
ostream& seekp (streamoff off, ios_base::seekdir way);

tellp

Returns the next write position in the output stream.

streampos tellp();

Example 3-2 – ostream member functions

Example 3-3 – binary file I/O: a practical example


C++ Cast Operators

Static Cast

A static_cast is used to convert one type to another.  Generally, static_casts should not be used and may be considered dangerous.  Often, conversions are automatic and the need to use a static_cast may imply that incorrect types are in use.  Static_cast are often used to convert one pointer type to another, or to convert an enumerated value to an integer value.

Example 4-1 – static_cast

Const Cast

A const_cast is used to add or remove constness to an expression.  Note, removing constness from a “pointed to” value may result in undefined behavior.

Example 4-2 – const_cast

Reinterpret Cast

A reinterpret_cast is used to cast one type to another.  It is most commonly used to treat one pointer type as another pointer type, or to treat a pointer type as an integer type and vice versa.  Note, this case type may be unsafe and to use it effectively, the sizes of the casted value and the casted type should match.

Example 4-3 – reinterpret_cast

Dynamic Cast

A dynamic_cast is used with inheritance to cast a base class pointer or reference to a derived class pointer or references.  This is called downcasting.  In order for this to succeed, the base class must be polymorphic (contains a virtual function).

Reference:  http://www.bogotobogo.com/cplusplus/upcasting_downcasting.php

Example 4-4 – dynamic_cast