- #include<stdio.h>
- #include<string>
- class Value
- {
- public:
- Value(int nVal) {
- m_nVal=nVal;
- printf("Call Value::Value(int nValue)/n");
- }
- ~Value() {
- printf("Call Value::~Value()/n");
- }
- Value& operator=(int nVal)
- {
- m_nVal=nVal;
- printf("Call Value::operator=/n");
- return *this;
- }
- void Dump() {
- printf("Value::m_nVal=%d/n",m_nVal);
- }
- protected:
- int m_nVal;
- };
- class Base
- {
- public:
- Base() {
- Init();
- }
- virtual ~Base() {
- Release();
- }
- virtual void Init() {
- printf("Call Base::Init()/n");
- }
- virtual void Release() {
- printf("Call Base::Release()/n");
- }
- virtual void Dump() {
- printf("Call Base::Dump()/n");
- }
- };
- class Derive:public Base
- {
- public:
- Derive() {
- printf("Call Derive::Derive()/n");
- }
- ~Derive() {
- printf("Call Derive::~Derive()/n");
- }
- virtual void Init() {
- m_Val=2;
- printf("Call Derive::Init()/n");
- }
- virtual void Release() {
- printf("Call Derive::Release()/n");
- }
- virtual void Dump() {
- m_Val.Dump();
- }
- protected:
- static Value m_Val;
- };
-
- Value Derive::m_Val=0;
-
- void DestroyObj(Base* pOb)
- {
- pOb->Dump();
- delete pOb;
- }
- void main()
- {
- Derive *pOb = new Derive;
- DestroyObj(pOb);
- }
输出:
Call Value::Value(int nValue) //静态变量构造
Call Base::Init() //基类构造
Call Derive::Derive() //派生类构造
Value::m_nVal=0 //多态
Call Derive::~Derive() //派生类析构
Call Base::Release() //基类析构
Call Value::~Value() //静态变量析构
阅读(1300) | 评论(0) | 转发(0) |