C ++单例与私有构造函数

编程入门 行业动态 更新时间:2024-10-26 20:23:27
本文介绍了C ++单例与私有构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要单一的应用程序生命周期,保证创建/销毁和静态访问它。

#include< iostream> ; #include< cstdlib> #define DISALLOW_COPY_AND_ASSIGN(TypeName)\ TypeName(const TypeName&); \ void operator =(const TypeName&) #define M()C :: sM() #define M2()C :: sM2 $ b using namespace std; class C { private: static C * s; 〜C(){cout< 〜C()< endl; } static C * instance(){ if(s == NULL){s = new C(); } cout<< instance()=< s<< endl;返回s; } static void cleanUp(){delete s; } void m(){cout<< m()< endl; } void m2(){cout<< m2()< endl; } DISALLOW_COPY_AND_ASSIGN(C); public: C(){ cout<< C()< endl; if(s == NULL){ s = this; atexit(& cleanUp); cout<< cleanUp is installed< endl; } else { cout<< cleanUp未安装< endl; } } void * operator new(size_t sz){ void * p = NULL; if(s == NULL){p = new char [sz]; } else {p = s; } cout<< new(<< sz<<)=< p < endl; return p; } void operator delete(void * p,size_t sz){ cout< delete(<< sz<<,<< p<<)< endl; if(p)delete [] static_cast< char *>(p); s = NULL; } void static sM(){cout<< sM()< endl; instance() - > m(); } void static sM2(){cout< sM2()< endl; instance() - > m2(); } }; C * C :: s = NULL; int main(){ M(); M2(); C * p1 = new C(); C * p2 = new C(); }

但我不知道如何摆脱g ++警告: p>

test.cpp:在静态成员函数'static void C :: operator delete(void *,size_t)': test.cpp:22:warning:删除'void *'未定义

of void *,析构函数开始在无限循环中调用自身。任何人都可以帮助我获得干净的代码,没有警告?

要删除的类型是 char * c>:

void operator delete(void * p,size_t sz){ cout< delete(<< sz<<,<< p<<)< endl; if(p)delete(char *)p; }

您的新版本中有一个错误:新的char [10]和新的char(10)之间的区别

也许应该是:

p = new char [sz];

,然后

code> delete [](char *)p;

----编辑:

纯粹主义的删除,牺牲人们学习的清晰度; P:

delete [] static_cast< char *>(p);

(*我真的很感激关于演员表的注意事项)

I need singleton with a application lifetime, guaranteed creation/destruction and static access to it.

#include <iostream> #include <cstdlib> #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&); \ void operator=(const TypeName&) #define M() C::sM() #define M2() C::sM2() using namespace std; class C { private: static C* s; ~C() { cout << "~C()" << endl; } static C* instance() { if (s==NULL) { s=new C(); } cout << "instance()=" << s << endl; return s; } static void cleanUp() { delete s; } void m() { cout << "m()" << endl; } void m2() { cout << "m2()" << endl; } DISALLOW_COPY_AND_ASSIGN(C); public: C() { cout << "C()" << endl; if (s==NULL) { s=this; atexit(&cleanUp); cout << "cleanUp is installed" << endl; } else { cout << "cleanUp is not installed" << endl; } } void* operator new(size_t sz) { void* p=NULL; if (s==NULL) { p=new char[sz]; } else { p=s; } cout << "new(" << sz << ")=" << p << endl; return p; } void operator delete(void* p, size_t sz) { cout << "delete(" << sz << "," << p << ")" << endl; if (p) delete[] static_cast<char*>(p); s=NULL; } void static sM() { cout << "sM()" << endl; instance()->m(); } void static sM2() { cout << "sM2()" << endl; instance()->m2(); } }; C* C::s = NULL; int main() { M(); M2(); C* p1 = new C(); C* p2 = new C(); }

But I don't know how to get rid of g++ warning:

test.cpp: In static member function 'static void C::operator delete(void*, size_t)': test.cpp:22: warning: deleting 'void*' is undefined

If I write C* instead of void*, destructor start calling itself in infinite loop. Can anybody help me to get clean code without warnings? C++98 of course.

解决方案

The type to delete is char*:

void operator delete(void* p, size_t sz) { cout << "delete(" << sz << "," << p << ")" << endl; if (p) delete (char*) p; }

There is a bug in your new: What's the difference between new char[10] and new char(10)

Maybe should be:

p=new char[sz];

and then

delete[] (char*)p ;

---- edited:

The delete for purists, sacrificing clarity to people learning ;P :

delete[] static_cast<char*>(p) ;

(*actually I appreciate the note about the cast)

更多推荐

C ++单例与私有构造函数

本文发布于:2023-10-31 05:44:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!