gpt4 book ai didi

c++ - 使类构造函数私有(private)

转载 作者:行者123 更新时间:2023-11-30 02:31:45 25 4
gpt4 key购买 nike

<分区>

我正在用 C++ 编写一个简单的垃圾收集器。我需要一个单例类 GarbageCollector 来处理不同类型的内存。我使用了 Meyer 的单例模式。但是当我尝试调用实例时,出现错误:

 error: ‘GarbageCollector::GarbageCollector(const GarbageCollector&)’ is private
GarbageCollector(const GarbageCollector&);
^

这是类定义。

class GarbageCollector //Meyers singleton (http://cpp-reference.ru/patterns/creational-patterns/singleton/)
{
public:
static GarbageCollector& instance(){
static GarbageCollector gc;
return gc;
}
size_t allocated_heap_memory;
size_t max_heap_memory;
private:
//Copying, = and new are not available to be used by user.
GarbageCollector(){};
GarbageCollector(const GarbageCollector&);
GarbageCollector& operator=(GarbageCollector&);
};

我使用以下行调用实例:auto gc = GarbageCollector::instance();

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com