简体   繁体   English

流畅的界面模式和std :: unique_ptr

[英]Fluent interface pattern and std::unique_ptr

I am playing with the fluent interface pattern. 我正在使用流畅的界面模式。

First, I wrote something like that: 首先,我写了类似的东西:

class C
{
public:
    C() { }

    C* inParam1(int arg1){ param1 = arg1; return this; }
    C* inParam2(int arg2){ param2 = arg2; return this; }

private:
    int param1;
    int param2;
}

Then I tried to use the std::unique_ptr, but then I realized that I do not know how to "shift" the pointer (this) along the chain. 然后我尝试使用std :: unique_ptr,但后来我意识到我不知道如何沿着链“移动”指针(this)。 I tried something like: 我尝试过类似的东西:

return std::move(this);

that of course does not work. 那当然不行。

How can I do this? 我怎样才能做到这一点? Are there any problems doing something like this? 做这样的事有什么问题吗?

In order to reply to comments like: "do not use pointers": there isn't (yet) any practical reason because I am doing this with pointers, is just something I wonder if can be done this way. 为了回复如下评论:“不要使用指针”:没有(还)任何实际原因,因为我用指针做这个,只是我想知道是否可以这样做。

Why do you have to return pointers at all? 为什么你必须返回指针?

class C
{
public:
    C create() { return C(); }

    C & inParam1(int arg1){ param1 = arg1; return *this; }
    C & inParam2(int arg2){ param2 = arg2; return *this; }

private:
    C() { }
    int param1;
    int param2;
};

I must admit I don't understand the purpose of that create function or why the constructor is private or how you actually create objects of this class at all. 我必须承认我不理解该创建函数的目的或者为什么构造函数是私有的或者你实际上如何创建这个类的对象。 In my understanding, the class should actually be like this: 根据我的理解,该课程应该是这样的:

class C
{
public:
    C() {}

    C & inParam1(int arg1){ param1 = arg1; return *this; }
    C & inParam2(int arg2){ param2 = arg2; return *this; }

private:        
    int param1;
    int param2;
};

And used like this: 像这样使用:

int main()
{
    C().inParam1(10).inParam2(20).whatever();
}

There can be only one std::unique_ptr instance for any given instance (because each would try to delete it and that can be done only once). 对于任何给定的实例,只能有一个std::unique_ptr实例(因为每个实例都会尝试删除它,并且只能执行一次)。 Therefore, while create can return a std::unique_ptr , the other two methods cannot. 因此,虽然create可以返回std::unique_ptr ,但其他两种方法都不能。

Is there any reason why you don't have the other methods return a reference rather than a pointer? 有没有理由为什么你没有其他方法返回引用而不是指针? And why don't you just have a public constructor? 你为什么不只是一个公共构造函数? I don't see the value of the create method in this example. 我在这个例子中没有看到create方法的值。

You missed one important think from the c++ fluent interface example : 您从c ++流畅的界面示例中错过了一个重要的想法:

//it doesn't make sense to chain after create(), so don't return *this

Therefore, you shouldn't return anything from your create() method. 因此,您不应该从create()方法返回任何内容。

However if you still want to return something, at least do not (mis-)use unique_ptr , and just return a new object : 但是如果你还想要返回一些东西,至少不要(误)使用unique_ptr ,只返回一个新对象:

C create() { return C(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM