近期比较关注设计模式的学习,用到模式继承、多态的运用是必不可少的, 今天遇到的情况值得记录一下:
原程序如下:(简单的工厂模式)
#include <iostream>
using namespace std;
// kejie leung
// 2008-4-13
//Factory Method Implement( in simple way )
class Document
...{
public:
Document();
~Document();
public:
virtual void Open();
virtual void Close();
};
Document::Document()
...{
cout<<"Document Construct ";
};
Document::~Document()
...{
cout<<"Document Disstruct ";
};
void Document::Open()
...{
cout<<"Document Open a file ";
}
void Document::Close()
...{
cout<<"Document Close a file ";
}
class MyDocument: public Document
...{
public:
MyDocument();
~MyDocument();
public:
void Open();
};
MyDocument::MyDocument()
...{
cout<<"MyDocument Construct ";
};
MyDocument::~MyDocument()
...{
cout<<"MyDocument Disstruct ";
};
void MyDocument::Open()
...{
cout<<"MyDocument Open a file ";
}
class Application
...{
public:
Application();
~Application();
public:
virtual Document * GetDocument() = 0;
virtual Document * CreateDocument() = 0;
};
//make the factory Create one product
Application::Application()
...{
cout<<"Application Construct ";
}
Application::~Application()
...{
cout<<"Application Disstruct ";
}
class MyApplication: public Application
...{
public:
MyApplication();
~MyApplication();
public:
Document * GetDocument();
Document * CreateDocument();
private:
MyDocument * doc;
};
MyApplication::MyApplication()
...{
cout<<"MyApplication Construct ";
doc = static_cast<MyDocument*>( CreateDocument() );
}
MyApplication::~MyApplication()
...{
if( doc )
delete doc;
cout<<"MyApplication Disstruct ";
}
Document * MyApplication::GetDocument()
...{
return doc;
}
Document * MyApplication::CreateDocument()
...{
return new MyDocument();
}
void main()
...{
Application * theApp = new MyApplication();
theApp->GetDocument()->Open();
delete theApp;
}
using namespace std;
// kejie leung
// 2008-4-13
//Factory Method Implement( in simple way )
class Document
...{
public:
Document();
~Document();
public:
virtual void Open();
virtual void Close();
};
Document::Document()
...{
cout<<"Document Construct ";
};
Document::~Document()
...{
cout<<"Document Disstruct ";
};
void Document::Open()
...{
cout<<"Document Open a file ";
}
void Document::Close()
...{
cout<<"Document Close a file ";
}
class MyDocument: public Document
...{
public:
MyDocument();
~MyDocument();
public:
void Open();
};
MyDocument::MyDocument()
...{
cout<<"MyDocument Construct ";
};
MyDocument::~MyDocument()
...{
cout<<"MyDocument Disstruct ";
};
void MyDocument::Open()
...{
cout<<"MyDocument Open a file ";
}
class Application
...{
public:
Application();
~Application();
public:
virtual Document * GetDocument() = 0;
virtual Document * CreateDocument() = 0;
};
//make the factory Create one product
Application::Application()
...{
cout<<"Application Construct ";
}
Application::~Application()
...{
cout<<"Application Disstruct ";
}
class MyApplication: public Application
...{
public:
MyApplication();
~MyApplication();
public:
Document * GetDocument();
Document * CreateDocument();
private:
MyDocument * doc;
};
MyApplication::MyApplication()
...{
cout<<"MyApplication Construct ";
doc = static_cast<MyDocument*>( CreateDocument() );
}
MyApplication::~MyApplication()
...{
if( doc )
delete doc;
cout<<"MyApplication Disstruct ";
}
Document * MyApplication::GetDocument()
...{
return doc;
}
Document * MyApplication::CreateDocument()
...{
return new MyDocument();
}
void main()
...{
Application * theApp = new MyApplication();
theApp->GetDocument()->Open();
delete theApp;
}
这时候问题来了,一运行结果如下:
Application Construct
MyApplication Construct
Document Construct
MyDocument Construct
MyDocument Open a file
Application Disstruct
Press any key to continue
-----------------
不知大家注意了没? MyDocument, Document, MyApplication都没有释放!!怎么回事呢?
再度一了下,觉得是指针问题:
Application * theApp = new MyApplication();
theApp->GetDocument()->Open();
theApp->GetDocument()->Open();
既然是父类指向的子类,那个 delete theApp 时,调用的为什么不是MyApplication的释构而只是Application的释构呢?无耐之下来点硬的:
delete static_cast<MyApplication*>(theApp);
这时候结果总于正常了:
Application Construct
MyApplication Construct
Document Construct
MyDocument Construct
MyDocument Open a file
MyDocument Disstruct
Document Disstruct
MyApplication Disstruct
Application Disstruct
Press any key to continue
MyApplication Construct
Document Construct
MyDocument Construct
MyDocument Open a file
MyDocument Disstruct
Document Disstruct
MyApplication Disstruct
Application Disstruct
Press any key to continue
但这下就奇了,难道要这么个delete法?在这个百思不得其解之时,正好看到CSDN上有人提问,为什么释构要设定为virtual~~~~呵呵,一语惊醒梦中人~~~~终于知道个中玄机了:
释构就是要设定为virtual!!~~~~
这样就可以轻轻松松地delete了~~~