我该如何调用一个函数并让我的构造函数保持私有?如果我让这个类是静态的,我需要声明一个编译器用来调用构造函数的对象名称,如果构造函数是私有的(对象也是无关的),那么它不能。这里是我尝试使用的代码(这是不编译):C++私人构造类
我想保持构造私有,因为我会在后面添加一个对象之前是做了很多检查,修改先前的对象时,所有提交变量不是唯一的,而是创建新的对象。
#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);
}
你能更清楚你的目标是什么吗?它看起来像你试图保留引荐的所有实例的列表。为什么你想让你的构造函数保持私有? – 2009-09-03 21:00:56