-
initialization lists
- The order in which the list entries are set down is determined by the declaration order of the members within the class declaration,
not the order within the initialization list
-
used in 4 case
1.When initializing a reference member
2.When initializing a const member
3.When invoking a base or member class constructor with a set of arguments
4.When you want to avoid creating un necessary temp objects.
- Topic
- EXCEPTION: Yes, to facilitate argument screening.
-
templates
- Note also that unlike class type declarations, the keyword struct cannot be used in place of typename when declaring type parameters.
- if the template 'definition' is included in a header file its called 'inclusion method'
-
Class Templates
- only members that are used are instantiated.
- we need to specify the TYPE of the class as a declaration. unlike in just function templates.
-
Specialization
-
partial
- any number of parameters / arguments can be specialised.
- only the MOST specialised funciton is picked. If nothing is found then onlly a template class is instantiated to fulful the request.
-
full
- you don't give any typename jsut the actual type example template <> class MyClass (int)
- If we have more than one template parameter in the class definition. EACH member def should also have BOTH the parameters.
ex:
template <typename T, typename CONTAINER = vector<T> >
class Stack
{
private: CONT container;
public: T pop();
...
};
template <typename T, typename CONT>
T Stack<T,CONT>::pop ()
{ ... }
-
const correctness
-
members
- "whenever the member function wants to guarantee that it won't make any observable changes to its this object.
-
Mutable
- Allows a const funciton to change a non observant member variable.
- STL
-
c++ Exception
-
4 alternatives
upon detecting
a problem
- terminate the program
- return a value representing
"error"
- return a legal value and leave the
program in an illegal state
- call a function supplied to
be called in case of "error"
-
Alternative views
of exceptions
- Designed to
support error handling
and other exceptional
conditions
- Designed to handle
only synchronous exceptions
- Asynchronous exceptions
should be handled in other way,
such as signal, etc. which may
be system-dependent
- The exception-handling
mechanism is a nonlocal
control structure based
on stack unwinding
- An exception is an
object of some class
representing an exceptional
occurrence
-
Resource management
- Exception and new
- Resource Exhaustion
-
auto_ptr
-
Pitfalls of auto_ptr
- Never let auto_ptr point to static or global object
- Never let two auto_ptrs point to the same object
- Never let auto_ptr point to array
- Never store auto_ptr in containers
- Use constructors and
destructors
- Exceptions in Constructors
- Exceptions and
Member Initializations
- Exceptions and Copying
- Exceptions that
are not errors
- Don't interrupt me
while I'm interrupting
-
Exception Specifications
-
Checking Exception
Specifications
- A virtual function may be
overridden by a function that has
an exception-specification at least
as restrictive as its own
exception-specification
- Empty exception specifications
could help to improve performance
-
Exception and
Efficiency
-
Overhead of Exception
- try block
- catch clause
- Cleanup of handled
exceptions
- Automatic and
temporary objects with
non-trival destructors
- Constructions of
objects with non-trival
destructors
- throw expression
- Enforcing expression
specification
- operator new
-
Two approaches
- Code approach
- Table approach
- Error-Handling
Alternatives
-
Standard Exceptions
- bad_alloc
- bad_cast
- bad_typeid
- bad_exception
- out_of_range
- invalid_argument
- overflow_error
- ios_base::failure
- Standard-Library
Exception Safety
- Destructor should
never throw exceptions
-
Distructor
- The destructor automatically calls the destructors for all member objects and all immediate nonvirtual base classes.
First, the destructor's body ({...}) is executed,
then the destructors for member objects are called in the reverse order that the member objects appear in the class body,
then the destructors for immediate base classes are called (in the reverse order they appear in the class declaration).
Virtual base classes are special—their destructors are called at the end of the most derived class's destructor (only).