C++私有构造的类

Non*_*biz 0 c++ static constructor private class

如何调用函数并保持构造函数的私有性?如果我使类静态,我需要声明一个编译器用来调用构造函数的对象名,如果构造函数是私有的,它也不能(对象也是无关的).这是我试图使用的代码(它不可编译):

我想保持构造函数是私有的,因为我稍后会在添加对象之前进行大量检查,在所有提交的变量不是唯一的而不是创建新对象时修改以前的对象.

#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <list>
#include <map>

using namespace std;
using namespace tr1;

class Referral
{
public:
    string url;
    map<string, int> keywords;

    static bool submit(string url, string keyword, int occurrences)
    {
        //if(Referrals.all.size == 0){
        //  Referral(url, keyword, occurrences);
        //}
    }

private:
    list<string> urls;

    Referral(string url, string keyword, int occurrences)
    {
        url = url;
        keywords[keyword] = occurrences;
        Referrals.all.push_back(this);
    }
};

struct All
{
    list<Referral> all;
}Referrals;

int main()
{
    Referral.submit("url", "keyword", 1);
}
Run Code Online (Sandbox Code Playgroud)

Jar*_*Par 6

拥有私有构造函数和静态工厂方法有什么问题?

class Example {
  Example() { ... }
public:
  static Example CreateExample() {
    return Example();
  }
};
Run Code Online (Sandbox Code Playgroud)