C++ & UNIX topics
(2010-05-16 12:17:37)
下一个
C++
(1) explicit
(a) used in constructor
class A
{
protected:
int m_i;
public:
[explicit] A(int i) : m_i(i) {}
}
this makes A a = 1 valid without explicit.
however, with explicit modifier, this is invalid. A a = (A)1 or A a= A(1) is valid.
(b) used in type conversion function.
class A
{
protected:
int m_i;
public
[explicit] operator int()
{
return m_i;
}
}
without explicit A a(1); int i = a; is valid.
with explicit, A a(1); int i = a; is invalid; int i=int(a); is valid.
(2) exception handling mechanism details.
(3) assignment operator & copy constructor
Foo& operator=(const Foo& foo)
{
if( this == &foo )
{
*this;
}
//clear member variable pointers.
//assign to foo member variables and allocate memory if necessary.
}
Foo( const Foo& foo )
{//as it's constructor, the member variables are not initialized.
}
LIB: iostream STL boost
(1) smart pointers: auto_ptr scoped_ptr shared_ptr
all about ownership.
scoped_ptr can't transfer ownership by =
auto_ptr transfers the ownership by = and the original auto_ptr becomes NULL.
shared_ptr shares ownership by maintaining reference counter inside.
(2) iterator category
input iterator
output iterator
forward iterator it++
bidirectional iterator it++ it--
random access iterator it += n
(3) function objects mechanism.
better than function pointer
(a) it can keep state, access extra member variables which can only achieved by global variables through function pointer.
(b) it can be overridden by virtual function mechanism. when this mechanism is used, the function object has to pass as reference instead of value. With this mechanism, function object can replace function pointer for all the cases.
struct Op
{
void operator() (int i) = 0;
};
struct MyOp
{
void operator()(int i) { cout << i; }
}
void CallOp(Op& op) //use reference as parameter type.
{
op(10);
}
main()
{
CallOp(MyOp()); // this is valid and will call MyOp::operator() instead.
}
(4) algorithms
algorithms are all template based functions working on iterators. it doesn't support virtual concept.