65,212
社区成员
发帖
与我相关
我的任务
分享static A& getInstance()
{
static A a;
return a;
}
[/quote]
智能指针,不是使用的引用计数么?这个a貌似没有被释放。我个人认为。如果有不对的地方,请指正,拜托了。[/quote]
有引用计数的智能指针是共享型的,当共享计数为0时自动释放。
a为什么没有被释放?你觉得这个静态对象的析构函数不会被调用吗?为什么?
这里有些讨论:http://stackoverflow.com/questions/1463707/c-singleton-vs-global-static-object[/quote]
/*
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton();
}
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public:
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
cout<<"Call the GC's destructor"<<endl;
delete m_Instance;
m_Instance = NULL;
}
}
};
static GC gc;
};
Singleton *Singleton::m_Instance = NULL;
int main(int argc, char *argv[])
{
Singleton *singletonObj = Singleton::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
谢谢你推荐的文章,但是,这个是个demo,那么cout<<"Call the GC's destructor"<<endl;会执行的,但是实际上,这个语句没有被执行;拜托解释一下。谢谢了。
你推荐的文章中,有人提到以下实现是最好的单例实现:
Printer & thePrinter() {
static Printer printer;
return printer;
}
但是,这种单例实现在实际应用中会遇到对象的拷贝问题,你有遇到过吗?
如果修改成以下是否可以:
Printer *thePrinter() {
static Printer printer;
return &printer;
}
希望能得到你的指导,谢谢。再次谢谢你耐心的指导。[/quote]
推荐使用只有三行代码的这种实现方式。这种方式在Qt的源码中也很常见。我一般都这样使用。
至于修改为返回指针,同样阻止不了复制行为,想想为什么?
要阻止复制行为,声明拷贝构造函数、赋值函数为私有即可。对于单例模式,由于不允许直接构造,因此构造函数也是私有的。[/quote]
再次感谢你耐心的回答,能给我这么详细的讲解,非常感谢。
对于你的回答,我还是有如下疑问,麻烦你能给我回答一下以下问题,拜托了。
> 至于修改为返回指针,同样阻止不了复制行为,想想为什么?
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
static Singleton m_Instance;
return &m_Instance;
}
/*static Singleton GetInstance()
{
static Singleton m_Instance;
return m_Instance;
}*/
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; };
Singleton(const Singleton &tmp) {}
Singleton operator =(const Singleton &tmp){}
int m_Test;
};
int main(int argc , char *argv [])
{
Singleton *singletonObj = Singleton ::GetInstance();
cout<<singletonObj->GetTest()<<endl;
/*Singleton singletonObj = Singleton ::GetInstance();
cout<<singletonObj.GetTest()<<endl;*/
}
按照你的回答,我进行了测试。麻烦确认我的测试代码是否正确。如果测试代码是正确的,我想确实避免的对象的拷贝。
2.#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton();
}
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public:
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
delete m_Instance;
m_Instance = NULL;
}
}
};
static GC gc;
};
Singleton *Singleton::m_Instance = NULL;
int main(int argc, char *argv[])
{
Singleton *singletonObj = Singleton::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
该代码中定义了static GC gc;在程序结束时,它会调用~GC进行析构吗?在我的实际测试中,它并没有调用析构函数,就连基本的构造函数都没有调用。但是,如果将static关键字去掉,它会调用构造函数,但是,析构函数还是没有调用。按照你的理解,能否说明一下。
以上,是小弟的问题,拜托回答一下。谢谢了。[/quote]
1,返回引用,否则每次调用返回都是原静态对象的副本,谈何单实例?
2,必须在类外面定义静态成员gc,否则对象都没有,谈何析构?static A& getInstance()
{
static A a;
return a;
}
[/quote]
智能指针,不是使用的引用计数么?这个a貌似没有被释放。我个人认为。如果有不对的地方,请指正,拜托了。[/quote]
有引用计数的智能指针是共享型的,当共享计数为0时自动释放。
a为什么没有被释放?你觉得这个静态对象的析构函数不会被调用吗?为什么?
这里有些讨论:http://stackoverflow.com/questions/1463707/c-singleton-vs-global-static-object[/quote]
/*
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton();
}
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public:
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
cout<<"Call the GC's destructor"<<endl;
delete m_Instance;
m_Instance = NULL;
}
}
};
static GC gc;
};
Singleton *Singleton::m_Instance = NULL;
int main(int argc, char *argv[])
{
Singleton *singletonObj = Singleton::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
谢谢你推荐的文章,但是,这个是个demo,那么cout<<"Call the GC's destructor"<<endl;会执行的,但是实际上,这个语句没有被执行;拜托解释一下。谢谢了。
你推荐的文章中,有人提到以下实现是最好的单例实现:
Printer & thePrinter() {
static Printer printer;
return printer;
}
但是,这种单例实现在实际应用中会遇到对象的拷贝问题,你有遇到过吗?
如果修改成以下是否可以:
Printer *thePrinter() {
static Printer printer;
return &printer;
}
希望能得到你的指导,谢谢。再次谢谢你耐心的指导。[/quote]
推荐使用只有三行代码的这种实现方式。这种方式在Qt的源码中也很常见。我一般都这样使用。
至于修改为返回指针,同样阻止不了复制行为,想想为什么?
要阻止复制行为,声明拷贝构造函数、赋值函数为私有即可。对于单例模式,由于不允许直接构造,因此构造函数也是私有的。[/quote]
再次感谢你耐心的回答,能给我这么详细的讲解,非常感谢。
对于你的回答,我还是有如下疑问,麻烦你能给我回答一下以下问题,拜托了。
> 至于修改为返回指针,同样阻止不了复制行为,想想为什么?
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
static Singleton m_Instance;
return &m_Instance;
}
/*static Singleton GetInstance()
{
static Singleton m_Instance;
return m_Instance;
}*/
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; };
Singleton(const Singleton &tmp) {}
Singleton operator =(const Singleton &tmp){}
int m_Test;
};
int main(int argc , char *argv [])
{
Singleton *singletonObj = Singleton ::GetInstance();
cout<<singletonObj->GetTest()<<endl;
/*Singleton singletonObj = Singleton ::GetInstance();
cout<<singletonObj.GetTest()<<endl;*/
}
按照你的回答,我进行了测试。麻烦确认我的测试代码是否正确。如果测试代码是正确的,我想确实避免的对象的拷贝。
2.#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton();
}
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public:
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
delete m_Instance;
m_Instance = NULL;
}
}
};
static GC gc;
};
Singleton *Singleton::m_Instance = NULL;
int main(int argc, char *argv[])
{
Singleton *singletonObj = Singleton::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
该代码中定义了static GC gc;在程序结束时,它会调用~GC进行析构吗?在我的实际测试中,它并没有调用析构函数,就连基本的构造函数都没有调用。但是,如果将static关键字去掉,它会调用构造函数,但是,析构函数还是没有调用。按照你的理解,能否说明一下。
以上,是小弟的问题,拜托回答一下。谢谢了。[/quote]
1. 无需实现以下函数,把花括弧去掉。
Singleton(const Singleton &tmp);
Singleton operator =(const Singleton &tmp);
你也可以让单例函数返回指针,跟返回引用区别不大,只是调用该对象的方法时使用的操作符不同。返回引用的话,可以很清楚的知道不用关心这个对象的释放问题。
2. 我也怀疑这种实现方式的有效性,不知道你在哪里看到的。
此外,《Modern C++ Design》一书中有singleton的实作,可找来一看。static A& getInstance()
{
static A a;
return a;
}
[/quote]
智能指针,不是使用的引用计数么?这个a貌似没有被释放。我个人认为。如果有不对的地方,请指正,拜托了。[/quote]
有引用计数的智能指针是共享型的,当共享计数为0时自动释放。
a为什么没有被释放?你觉得这个静态对象的析构函数不会被调用吗?为什么?
这里有些讨论:http://stackoverflow.com/questions/1463707/c-singleton-vs-global-static-object[/quote]
/*
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton();
}
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public:
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
cout<<"Call the GC's destructor"<<endl;
delete m_Instance;
m_Instance = NULL;
}
}
};
static GC gc;
};
Singleton *Singleton::m_Instance = NULL;
int main(int argc, char *argv[])
{
Singleton *singletonObj = Singleton::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
谢谢你推荐的文章,但是,这个是个demo,那么cout<<"Call the GC's destructor"<<endl;会执行的,但是实际上,这个语句没有被执行;拜托解释一下。谢谢了。
你推荐的文章中,有人提到以下实现是最好的单例实现:
Printer & thePrinter() {
static Printer printer;
return printer;
}
但是,这种单例实现在实际应用中会遇到对象的拷贝问题,你有遇到过吗?
如果修改成以下是否可以:
Printer *thePrinter() {
static Printer printer;
return &printer;
}
希望能得到你的指导,谢谢。再次谢谢你耐心的指导。[/quote]
推荐使用只有三行代码的这种实现方式。这种方式在Qt的源码中也很常见。我一般都这样使用。
至于修改为返回指针,同样阻止不了复制行为,想想为什么?
要阻止复制行为,声明拷贝构造函数、赋值函数为私有即可。对于单例模式,由于不允许直接构造,因此构造函数也是私有的。[/quote]
再次感谢你耐心的回答,能给我这么详细的讲解,非常感谢。
对于你的回答,我还是有如下疑问,麻烦你能给我回答一下以下问题,拜托了。
> 至于修改为返回指针,同样阻止不了复制行为,想想为什么?
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
static Singleton m_Instance;
return &m_Instance;
}
/*static Singleton GetInstance()
{
static Singleton m_Instance;
return m_Instance;
}*/
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; };
Singleton(const Singleton &tmp) {}
Singleton operator =(const Singleton &tmp){}
int m_Test;
};
int main(int argc , char *argv [])
{
Singleton *singletonObj = Singleton ::GetInstance();
cout<<singletonObj->GetTest()<<endl;
/*Singleton singletonObj = Singleton ::GetInstance();
cout<<singletonObj.GetTest()<<endl;*/
}
按照你的回答,我进行了测试。麻烦确认我的测试代码是否正确。如果测试代码是正确的,我想确实避免的对象的拷贝。
2.#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton();
}
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public:
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
delete m_Instance;
m_Instance = NULL;
}
}
};
static GC gc;
};
Singleton *Singleton::m_Instance = NULL;
int main(int argc, char *argv[])
{
Singleton *singletonObj = Singleton::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
该代码中定义了static GC gc;在程序结束时,它会调用~GC进行析构吗?在我的实际测试中,它并没有调用析构函数,就连基本的构造函数都没有调用。但是,如果将static关键字去掉,它会调用构造函数,但是,析构函数还是没有调用。按照你的理解,能否说明一下。
以上,是小弟的问题,拜托回答一下。谢谢了。static A& getInstance()
{
static A a;
return a;
}
[/quote]
智能指针,不是使用的引用计数么?这个a貌似没有被释放。我个人认为。如果有不对的地方,请指正,拜托了。[/quote]
有引用计数的智能指针是共享型的,当共享计数为0时自动释放。
a为什么没有被释放?你觉得这个静态对象的析构函数不会被调用吗?为什么?
这里有些讨论:http://stackoverflow.com/questions/1463707/c-singleton-vs-global-static-object[/quote]
/*
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton();
}
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public:
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
cout<<"Call the GC's destructor"<<endl;
delete m_Instance;
m_Instance = NULL;
}
}
};
static GC gc;
};
Singleton *Singleton::m_Instance = NULL;
int main(int argc, char *argv[])
{
Singleton *singletonObj = Singleton::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
谢谢你推荐的文章,但是,这个是个demo,那么cout<<"Call the GC's destructor"<<endl;会执行的,但是实际上,这个语句没有被执行;拜托解释一下。谢谢了。
你推荐的文章中,有人提到以下实现是最好的单例实现:
Printer & thePrinter() {
static Printer printer;
return printer;
}
但是,这种单例实现在实际应用中会遇到对象的拷贝问题,你有遇到过吗?
如果修改成以下是否可以:
Printer *thePrinter() {
static Printer printer;
return &printer;
}
希望能得到你的指导,谢谢。再次谢谢你耐心的指导。[/quote]
推荐使用只有三行代码的这种实现方式。这种方式在Qt的源码中也很常见。我一般都这样使用。
至于修改为返回指针,同样阻止不了复制行为,想想为什么?
要阻止复制行为,声明拷贝构造函数、赋值函数为私有即可。对于单例模式,由于不允许直接构造,因此构造函数也是私有的。static A& getInstance()
{
static A a;
return a;
}
[/quote]
智能指针,不是使用的引用计数么?这个a貌似没有被释放。我个人认为。如果有不对的地方,请指正,拜托了。[/quote]
有引用计数的智能指针是共享型的,当共享计数为0时自动释放。
a为什么没有被释放?你觉得这个静态对象的析构函数不会被调用吗?为什么?
这里有些讨论:http://stackoverflow.com/questions/1463707/c-singleton-vs-global-static-object[/quote]
/*
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton();
}
return m_Instance;
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 10; }
static Singleton *m_Instance;
int m_Test;
// This is important
class GC
{
public:
~GC()
{
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
cout<<"Call the GC's destructor"<<endl;
delete m_Instance;
m_Instance = NULL;
}
}
};
static GC gc;
};
Singleton *Singleton::m_Instance = NULL;
int main(int argc, char *argv[])
{
Singleton *singletonObj = Singleton::GetInstance();
cout<<singletonObj->GetTest()<<endl;
return 0;
}
谢谢你推荐的文章,但是,这个是个demo,那么cout<<"Call the GC's destructor"<<endl;会执行的,但是实际上,这个语句没有被执行;拜托解释一下。谢谢了。
你推荐的文章中,有人提到以下实现是最好的单例实现:
Printer & thePrinter() {
static Printer printer;
return printer;
}
但是,这种单例实现在实际应用中会遇到对象的拷贝问题,你有遇到过吗?
如果修改成以下是否可以:
Printer *thePrinter() {
static Printer printer;
return &printer;
}
希望能得到你的指导,谢谢。再次谢谢你耐心的指导。static A& getInstance()
{
static A a;
return a;
}
[/quote]
智能指针,不是使用的引用计数么?这个a貌似没有被释放。我个人认为。如果有不对的地方,请指正,拜托了。[/quote]
有引用计数的智能指针是共享型的,当共享计数为0时自动释放。
a为什么没有被释放?你觉得这个静态对象的析构函数不会被调用吗?为什么?
这里有些讨论:http://stackoverflow.com/questions/1463707/c-singleton-vs-global-static-object#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton* Instance();
protected:
Singleton();//此处声明为了保护 则无法实现继承
private:
static Singleton* _instance;
};
#endif //~_SINGLETON_H_
#include "Singleton.h"
#include <iostream>
using namespace std;
Singleton* Singleton::_instance = 0;
Singleton::Singleton()
{
cout<<"Singleton...."<<endl;
}
Singleton* Singleton::Instance()
{
if (_instance == 0)
{
_instance = new Singleton();
}
return _instance;
}
//创建全局唯一一个对象
int main(int argc,char* argv[])
{
Singleton* sgn = Singleton::Instance();
// Singleton* sgn = Singleton::Instance();
//不会再创建了 因为_instance=0才创建一旦创建就不在new 了
system("pause");
return 0;
}static A& getInstance()
{
static A a;
return a;
}
[/quote]
智能指针,不是使用的引用计数么?这个a貌似没有被释放。我个人认为。如果有不对的地方,请指正,拜托了。static A& getInstance()
{
static A a;
return a;
}