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);
}
How can I call a function and keep my constructor private? If I make the class static, I need to declare an object name which the compiler uses to call the constructor, which it cannot if the constructor is private (also the object would be extraneous). Here is the code I am attempting to use (it is not compilable):
I want to keep the constructor private because I will later be doing a lot of checks before adding an object, modifying previous objects when all submitted variables are not unique rather than creating new objects.
#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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用私有构造函数和静态工厂方法有什么问题?
What's wrong with having a private constructor and a static factory method?
根据您的
main
代码,我认为您要拍摄的是一个单例,它看起来像:然后您在
main
中的调用将看起来像:另一种可能性是您希望保留一个推荐列表,在这种情况下您可以使用
struct
和它们的列表来完成您正在寻找的内容:然后您的
main
中的调用如下所示:Based on your
main
code I think what you're shooting for is a singleton, which would look something like:Then your call in
main
would look like:Another possibility is that you're looking to keep a list of Referrals around, in which case you can use a
struct
and a list of them to accomplish what you're looking for:Then your call in
main
would look like:首先,您需要将 Submit 设为静态函数。那么你可以直接
说不创建 Referral 实例。
然后,在您的 Submit 函数中,您只是创建了一个几乎立即消失的临时 Referral 对象。可能您想要做的是使用 new 动态创建一个实例。根据您希望如何管理此操作,您可能希望将推送到列表的代码移至“提交”。
最后,我会将您的推荐实例列表设为静态成员变量,而不是您现在的方式。
(此外,通过引用传递这些字符串参数可能是一个好主意。)
First, you need to make Submit a static function. Then you can just say
without creating a Referral instance.
Then, in your Submit function, you're only creating a temporary Referral object that disappears almost immediately. Probably what you want to do is create an instance dynamically with new. Depending on how you want to manage this, you may want to move the code pushing onto the list into Submit.
Lastly, I would make your list of Referral instances a static member variable rather than how you have it now.
(Also, passing those string arguments by reference would probably be a good idea.)
虽然整个代码有一些味道,但您只需进行与您的问题无关的细微更改即可使其工作。
为了使其能够编译,我删除了正则表达式包含(我没有使用支持 C++0x 的编译器)和“使用命名空间 tr1”。将构造函数实现移至 Referral 全局对象的定义之后。更改 .当您引用静态方法时,主函数中的 :: 。
现在,从设计的角度来看,代码有一种恶臭。如果确实想要在其中添加 Referral 对象的全局列表,请考虑将其设为 Referral 类的私有静态属性,以便您可以对其进行更多控制(只有 Referral 类中的方法可能会破坏内容)。将所有属性设为私有,并仅提供用户代码所需功能的访问器(在大多数情况下只读访问就足够了)。在构造函数中使用初始化列表,并按照类定义中出现的顺序初始化所有成员。
一切都解决了,它仍然有一些气味。静态函数创建类的实例,但构造函数是将其自身包含在映射中的实例(??)如果构造函数不与映射交互,并且 Submit() 方法将创建,那么会更有意义对象,然后将其包含在列表中...
我认为您可能会从表达您打算做什么中受益,这里的许多人会帮助您选择设计及其原因。
While the whole code has some smell around, you can make it work just by making slight changes that are unrelated to your question.
To make it compile, I have removed the regex include (I am not using a compiler with C++0x support) and the 'using namespace tr1'. Move the constructor implementation after the definition of the Referral global object. Change the . for a :: in the main function when you refer to a static method.
Now, from a design point of view the code has a stench to it. If really want to have a global list where you add your Referral objects, consider making it a private static attribute of the Referral class so that you can have a little more control over it (only methods in the Referral class could break the contents). Make all your attributes private and provide only accessors to the functionality that user code will need (read-only access can suffice in most cases). Use initialization lists in your constructors, and initialize all members there in the same order they appear in the class definition.
With all that fixed, it still has some smell to it. The static function creates an instance of the class but the constructor is the one that includes itself in the map (??) It would make a little more sense if the constructor did not interact with the map, and the submit() method would create the object and then include it in the list...
I think you might benefit from expressing what you intend to do, many people here will help you both with design choices and reasons for them.