Sunday, September 09, 2007

Effecitve C++ (14,16,17)

item 14: Think carefully about copying behavior in resource-managing classes
  • Different between assignment and initialization.
  • Using "member initialization list". Must be used: data members are const or are references.
void lock(Mutex *pm);
void unlock(Mutex *pm);

We want to make sure we never forget to unlock a Mutex:
class Lock{
public:
explicit Lock(Mutex *pm)
:mutexPtr(pm)
{ lock(mutexPtr); }

~lock(){unlock(mutexPtr);}

private:
Mutex *mutexPtr;
};

what happened when Lock object is copied?
Lock ml2(ml1);

item 16: Use the same form in corresponding uses of new and delete

when we use 'new', two things happen. First, memory is allocated; Second, one or more constructors are called for that memory. Two same things happen for 'delete'. If you use brackets in your use of delete, delete assumes an array is pointed to. Otherwise, it assumes that a single object is pointed to.

item 17: Store newed objects in smart pointers in standalone statements

u...

No comments:

Post a Comment