都是virtual惹的祸
来源:互联网 发布:数控编程难不难 编辑:程序博客网 时间:2024/04/30 03:04
近期比较关注设计模式的学习,用到模式继承、多态的运用是必不可少的, 今天遇到的情况值得记录一下:
原程序如下:(简单的工厂模式)
#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了~~~
- 都是virtual惹的祸
- Virtual PC 2004下MS-DOS 7.1安装DOSADD实现文件共享(都是微软的策略惹的祸!)
- 都是‘/’惹的祸
- 都是Gmail惹的祸。
- 都是Gmail惹的祸
- 都是咖啡惹的祸~
- 都是公款惹的祸
- 都是进步惹的祸!
- 都是"魔波"惹的祸
- 都是一句话惹的祸
- 都是内存惹的祸
- 都是base惹的祸
- 都是习惯惹的祸
- 都是包包惹的祸
- 都是@@IDENTITY惹的祸
- 都是诺顿惹的祸
- 都是心态惹的祸!
- 都是名字惹的祸。。。。。。
- Nutch 0.8笔记(1)--必知必会
- Collection List/Set/Map 的区别和联系
- 二分法
- Hadoop笔记--不需要分布式编程经验地分布式编程
- 网络信息探测技巧
- 都是virtual惹的祸
- QuickServer--在吵闹的环境里快速搭建自己的TcpServer(Pragmatic系列)
- 盗号木马Trojan-PSW.Win32.OLGame.vdh
- XFire 最新生火指南(上)
- [Bug 5725] New: Code for undocumented SwitchToThisWindow function does not reflect empirical evidence
- Chapter2:使用强类型数据集和TableAdapter
- ShellCode编写实例—突破防火墙的ShellCode
- XFire 最新生火指南(下)
- VMware中共享window文件(另类方法)