Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“测试时发布”;带有new和delete(C+;+;)的单例; 我在C++中已经阅读了有关新的和删除的内容。有一段代码实现了单例模式。我已经测试了这段代码: #include <iostream> #include <memory> class Singleton { static Singleton *instance; static std::size_t refcount; std::string _s; public: void setS(std::string s) { _s = s; } std::string getS() { return _s; } static void *operator new(std::size_t nbytes) throw (std::bad_alloc) { std::cout << "operator new" << std::endl; if (instance == nullptr) { std::cout << "operator new nullptr" << std::endl; instance = ::new Singleton; // Use the default allocator } refcount++; return instance; } static void operator delete(void *p) { std::cout << "operator delete" << std::endl; if (--refcount == 0) { std::cout << "operator delete" << refcount << std::endl; ::delete instance; instance = nullptr; } } }; Singleton *Singleton::instance = nullptr; std::size_t Singleton::refcount = 0; int main() { Singleton* s = new Singleton; //Singleton* t = new Singleton; s->setS("string s"); std::cout << "s " << s->getS() << std::endl; Singleton* t = new Singleton; std::cout << "t " << t->getS() << std::endl; return 0; }_C++_Singleton_New Operator_Delete Operator - Fatal编程技术网

“测试时发布”;带有new和delete(C+;+;)的单例; 我在C++中已经阅读了有关新的和删除的内容。有一段代码实现了单例模式。我已经测试了这段代码: #include <iostream> #include <memory> class Singleton { static Singleton *instance; static std::size_t refcount; std::string _s; public: void setS(std::string s) { _s = s; } std::string getS() { return _s; } static void *operator new(std::size_t nbytes) throw (std::bad_alloc) { std::cout << "operator new" << std::endl; if (instance == nullptr) { std::cout << "operator new nullptr" << std::endl; instance = ::new Singleton; // Use the default allocator } refcount++; return instance; } static void operator delete(void *p) { std::cout << "operator delete" << std::endl; if (--refcount == 0) { std::cout << "operator delete" << refcount << std::endl; ::delete instance; instance = nullptr; } } }; Singleton *Singleton::instance = nullptr; std::size_t Singleton::refcount = 0; int main() { Singleton* s = new Singleton; //Singleton* t = new Singleton; s->setS("string s"); std::cout << "s " << s->getS() << std::endl; Singleton* t = new Singleton; std::cout << "t " << t->getS() << std::endl; return 0; }

“测试时发布”;带有new和delete(C+;+;)的单例; 我在C++中已经阅读了有关新的和删除的内容。有一段代码实现了单例模式。我已经测试了这段代码: #include <iostream> #include <memory> class Singleton { static Singleton *instance; static std::size_t refcount; std::string _s; public: void setS(std::string s) { _s = s; } std::string getS() { return _s; } static void *operator new(std::size_t nbytes) throw (std::bad_alloc) { std::cout << "operator new" << std::endl; if (instance == nullptr) { std::cout << "operator new nullptr" << std::endl; instance = ::new Singleton; // Use the default allocator } refcount++; return instance; } static void operator delete(void *p) { std::cout << "operator delete" << std::endl; if (--refcount == 0) { std::cout << "operator delete" << refcount << std::endl; ::delete instance; instance = nullptr; } } }; Singleton *Singleton::instance = nullptr; std::size_t Singleton::refcount = 0; int main() { Singleton* s = new Singleton; //Singleton* t = new Singleton; s->setS("string s"); std::cout << "s " << s->getS() << std::endl; Singleton* t = new Singleton; std::cout << "t " << t->getS() << std::endl; return 0; },c++,singleton,new-operator,delete-operator,C++,Singleton,New Operator,Delete Operator,为什么不打印“字符串s”?如果我更改注释行,t可以打印出“字符串s”。语句new Singleton将调用operator new以获取存储,然后使用默认构造函数初始化对象的非静态成员 由于\u s不是静态的,因此每次创建新的单例时都会(重新)初始化它。因此将导致t的空白字符串 通过这种方式很可能会重用\u s成员的空间。如何考虑堆栈分配的实例?@user4759923:*s和*t是相同的,只有一个。但是对于Singleton没有运算符==。如果定义此运算符,则结果取决于您。@user26721

为什么不打印“字符串s”?如果我更改注释行,t可以打印出“字符串s”。语句
new Singleton
将调用
operator new
以获取存储,然后使用默认构造函数初始化对象的非静态成员

由于
\u s
不是静态的,因此每次创建新的
单例时都会(重新)初始化它。因此将导致
t
的空白字符串


通过这种方式很可能会重用
\u s
成员的空间。

如何考虑堆栈分配的实例?@user4759923:*s和*t是相同的,只有一个。但是对于Singleton没有运算符==。如果定义此运算符,则结果取决于您。@user2672165:stack不会使用运算符new来分配可用的存储。这只是调用堆栈上分配的存储上的构造函数。所以堆栈上的对象不同于堆上的对象。堆上只有一个对象。存储是相同的,但每个新表达式(重新)初始化每个非静态成员时都会调用构造函数。
operator new
operator new nullptr
s string s
operator new
t