C++里,对象的这3个函数的调用顺序为:基类构造--〉本构造--〉赋值--〉本析构--〉基类析构
class A
{
public :
A(int i = 0){ m_i = i;cout
《m_i 《 "A::A()"
《 endl;}
~A(){cout 《 m_i《"A::~A()\n";}
void f(){cout 《 m_i 《 "A::f()\n";}
private:
int m_i;
};
class B:public A
{
public:
A m_A;
B(int i):m_A(i)
{
cout《 "B::B()\n";
}
~B(){cout 《"B::~B()\n";}
void f(){cout 《 "B::f()\n";}
};
void main( void )
{
A *pa = new B(10);
pa--〉f();
delete pa;
return;
}
class CLock
{
public:
void Lock()
{EnterCriticalSection(&m_sec);}
void
Unlock() {LeaveCriticalSection(&m_sec);}
CLock ()
{InitializeCriticalSection(&m_sec);}
~ CLock ()
{DeleteCriticalSection(&m_sec);}
CRITICAL_SECTION m_sec;
};
class CAutoLock
{
public:
CAutoLock(CLock * lpLock) :
m_pLock (lpLock)
{
m_pLock ->Lock();
}
~CAutoLock()
{
m_pLock ->Unlock();
}
protected:
private:
CLock * m_pLock;
};
CLock m_lock;
CAutoLock(&m_lock);
Copyright 2011-2020 © MallocFree. All rights reserved.