如何使用私人构造函数得出基类?

发布于 2025-01-30 17:44:53 字数 1502 浏览 2 评论 0 原文

我想设计带有私有构造函数的基类:

#pragma once
#include <bits/stdc++.h>
// base class for singleton
template<typename T>
class Singleton {
 public:
  virtual ~Singleton() {}
  static Singleton& Inst(const T& t) {
    const auto & it = ss_map_.find(t);
    if (it == ss_map_.end()) { printf("Singleton Inst Failed\n"); exit(1); }
    else return *(it->second);
  }

  static void Malloc(const std::vector<T>& v) { 
    for (const auto & i : v) ss_map_[i] = new Singleton(i);
  }     

  static void Release() { 
    for (auto & [_, s] : ss_map_) delete s;
  }     

  static void Release(const T& t) { 
    const auto & it = ss_map_.find(t);
    if (it == ss_map_.end()) { printf("Singleton Release Failed\n"); exit(1); } 
    delete it->second;
    ss_map_.erase(it);
  }     

 protected:
  inline static std::unordered_map<const T, Singleton*> ss_map_;
 private:
  Singleton(const T&) {}
  Singleton& operator =(const Singleton&)=delete;
  Singleton(const Singleton&) = delete;
  Singleton(const Singleton&&) = delete;
};

我将其作为模板类,可以让任何类型的派生类构造。

派生的类看起来像这样:

class A : public Singleton<int> {
 friend class Singleton;
 private:
  A(int a) : Singleton<int>(a) {}  // PROBLEM HERE, the complier failed to complie.
}

请参阅《代码: a类A 的构造函数》中的注释,而compleier错误失败了:

基类“ Singleton”具有私人构造函数

我该如何使 a 工作?

I want to design a base class with private constructor, like this:

#pragma once
#include <bits/stdc++.h>
// base class for singleton
template<typename T>
class Singleton {
 public:
  virtual ~Singleton() {}
  static Singleton& Inst(const T& t) {
    const auto & it = ss_map_.find(t);
    if (it == ss_map_.end()) { printf("Singleton Inst Failed\n"); exit(1); }
    else return *(it->second);
  }

  static void Malloc(const std::vector<T>& v) { 
    for (const auto & i : v) ss_map_[i] = new Singleton(i);
  }     

  static void Release() { 
    for (auto & [_, s] : ss_map_) delete s;
  }     

  static void Release(const T& t) { 
    const auto & it = ss_map_.find(t);
    if (it == ss_map_.end()) { printf("Singleton Release Failed\n"); exit(1); } 
    delete it->second;
    ss_map_.erase(it);
  }     

 protected:
  inline static std::unordered_map<const T, Singleton*> ss_map_;
 private:
  Singleton(const T&) {}
  Singleton& operator =(const Singleton&)=delete;
  Singleton(const Singleton&) = delete;
  Singleton(const Singleton&&) = delete;
};

I make it as a template class, which can let derived class construct with any type.

The derived class looks like this:

class A : public Singleton<int> {
 friend class Singleton;
 private:
  A(int a) : Singleton<int>(a) {}  // PROBLEM HERE, the complier failed to complie.
}

Please see the notes in the code: the constructor of class A fails with the complier error:

base class 'Singleton' has private constructor

How can I make class A work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2025-02-06 17:44:53

您已经以错误的方式定义了您的朋友关系。 base类需要声明类A 一个朋友,以便提供对其私有构造函数的访问:

#include <iostream>
#include <vector>
#include <unordered_map>

template<typename T>
class Singleton {
    friend class A; // Now, "class A" will have access to private members
public:
// ... and so forth ...

注:另请参阅: Why我不应该#include&lt; lt; stdc ++。

You have defined your friend relationship the wrong way round. The base class needs to declare class A a friend in order to provide access to its private constructor:

#include <iostream>
#include <vector>
#include <unordered_map>

template<typename T>
class Singleton {
    friend class A; // Now, "class A" will have access to private members
public:
// ... and so forth ...

Note: See also: Why should I not #include <bits/stdc++.h>?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文