设计模式中的单例模式的代码为什么析构函数会多次被调用,而构造函数只调用一次?
2011-04-11 23:29
323 查看
/********************************************************************
created: 2006/07/20
filename: Singleton.h
author: 李创
http://www.cppblog.com/converse/
purpose: Singleton模式的演示代码
*********************************************************************/
#ifndef SINGLETON_H
#define SINGLETON_H
class Singleton
{
public:
Singleton();
~Singleton();
// 静态成员函数,提供全局访问的接口
static Singleton* GetInstancePtr();
static Singleton GetInstance();
void Test();
private:
// 静态成员变量,提供全局惟一的一个实例
static Singleton* m_pStatic;
};
#endif
/********************************************************************
created: 2006/07/20
filename: Singleton.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: Singleton模式的演示代码
*********************************************************************/
#include "Singleton.h"
#include <iostream>
// 类的静态成员变量要在类体外进行定义
Singleton* Singleton::m_pStatic = NULL;
Singleton::Singleton()
{
std::cout << "构造函数被调用/n";
}
Singleton::~Singleton()
{
std::cout << "析构函数被调用/n";
}
Singleton* Singleton::GetInstancePtr()
{
if (NULL == m_pStatic)
{
m_pStatic = new Singleton();
}
return m_pStatic;
}
Singleton Singleton::GetInstance()
{
return *GetInstancePtr();
}
void Singleton::Test()
{
std::cout << "Test函数被调用!/n";
}
/********************************************************************
created: 2006/07/20
filename: Main.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: Singleton模式的测试代码
*********************************************************************/
#include "Singleton.h"
#include <stdlib.h>
//析构函数只被调用一次的情形:
int main()
{
// 不用初始化类对象就可以访问了
for( int i =0; i < 10; i++)
{
Singleton::GetInstancePtr()->Test();//程序执行完这一句之后析构函数被调用一次,如果是第一次,构造函数被调用
//并且构造函数只被调用一次
}
return 0;
}
程序运行结果:
构造函数被调用
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
析构函数多次被调用的情况:
void Test();
int g_iTest = 0;
int main()
{
for( int i = 0; i < 5; i++ )//析构函数5次被调用
{
Test();
}
system("pause");
return 0;
}
void Test()
{
g_iTest++;
printf( "第 %d 次调用成员函数", g_iTest );
Singleton::GetInstance().Test();
printf( "/n/n" );
}
第 1 次调用成员函数(构造函数被调用)Test!
(析构函数被调用)
第 2 次调用成员函数Test!
(析构函数被调用)
第 3 次调用成员函数Test!
(析构函数被调用)
第 4 次调用成员函数Test!
(析构函数被调用)
第 5 次调用成员函数Test!
(析构函数被调用)
请按任意键继续. . .
created: 2006/07/20
filename: Singleton.h
author: 李创
http://www.cppblog.com/converse/
purpose: Singleton模式的演示代码
*********************************************************************/
#ifndef SINGLETON_H
#define SINGLETON_H
class Singleton
{
public:
Singleton();
~Singleton();
// 静态成员函数,提供全局访问的接口
static Singleton* GetInstancePtr();
static Singleton GetInstance();
void Test();
private:
// 静态成员变量,提供全局惟一的一个实例
static Singleton* m_pStatic;
};
#endif
/********************************************************************
created: 2006/07/20
filename: Singleton.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: Singleton模式的演示代码
*********************************************************************/
#include "Singleton.h"
#include <iostream>
// 类的静态成员变量要在类体外进行定义
Singleton* Singleton::m_pStatic = NULL;
Singleton::Singleton()
{
std::cout << "构造函数被调用/n";
}
Singleton::~Singleton()
{
std::cout << "析构函数被调用/n";
}
Singleton* Singleton::GetInstancePtr()
{
if (NULL == m_pStatic)
{
m_pStatic = new Singleton();
}
return m_pStatic;
}
Singleton Singleton::GetInstance()
{
return *GetInstancePtr();
}
void Singleton::Test()
{
std::cout << "Test函数被调用!/n";
}
/********************************************************************
created: 2006/07/20
filename: Main.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: Singleton模式的测试代码
*********************************************************************/
#include "Singleton.h"
#include <stdlib.h>
//析构函数只被调用一次的情形:
int main()
{
// 不用初始化类对象就可以访问了
for( int i =0; i < 10; i++)
{
Singleton::GetInstancePtr()->Test();//程序执行完这一句之后析构函数被调用一次,如果是第一次,构造函数被调用
//并且构造函数只被调用一次
}
return 0;
}
程序运行结果:
构造函数被调用
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
Test函数被调用!
析构函数多次被调用的情况:
void Test();
int g_iTest = 0;
int main()
{
for( int i = 0; i < 5; i++ )//析构函数5次被调用
{
Test();
}
system("pause");
return 0;
}
void Test()
{
g_iTest++;
printf( "第 %d 次调用成员函数", g_iTest );
Singleton::GetInstance().Test();
printf( "/n/n" );
}
第 1 次调用成员函数(构造函数被调用)Test!
(析构函数被调用)
第 2 次调用成员函数Test!
(析构函数被调用)
第 3 次调用成员函数Test!
(析构函数被调用)
第 4 次调用成员函数Test!
(析构函数被调用)
第 5 次调用成员函数Test!
(析构函数被调用)
请按任意键继续. . .
相关文章推荐
- Android中bindService的细节之三:多次调用bindService(),为什么onBind()只执行一次?
- [js高手之路] 设计模式系列课程 - jQuery的链式调用与灵活的构造函数
- 为什么构造函数和析构函数中不应该调用虚函数
- 读js设计模式有感->构造函数内调用自己公用方法函数
- 析构函数显示调用(系统会默认在调用一次)而构造函数不允许显示调用
- vector用erase删除元素时,为什么被删元素的析构函数会被调用多次?
- 从一小段代码看构造函数和析构函数的调用
- 为什么不要在构造函数和析构函数中调用虚函数?
- 《Effective C++ 》条款9:永远不要在构造函数或析构函数中调用虚函数
- 对象初始化以及在构造函数和析构函数中调用virtual函数的问题
- C++继承中析构函数 构造函数的调用顺序以及虚析构函数
- 设计模式之抽象工厂模式代码示例
- 为什么要学设计模式
- ListView的观察者模式的应用,以及调用notifyDataSetChanged()方法时,为什么会刷新ListView
- 设计模式之访问者模式代码示例
- 构造函数,析构函数内调用虚函数问题
- 为什么构造函数不能声明为虚函数,析构函数可以
- c++ 构造函数,复制构造函数,赋值构造函数,析构函数调用时机
- 设计模式简单代码之Visitor模式
- 设计模式简单代码之Proxy模式(买房子篇)