I want to create a class only for setting params for function. I'm using a fluent interface for that. Some function returns the object to setting params and real code will execute in the destructor of this object.
First of all, is it a good idea? Is this a common way to improve code readability when I have a lot of parameters? Or maybe I should use another way to do that? I would be glad to advise on this.
If it's okay, how can I prevent accidentally saving an object to a local variable? In this case, the destructor will be called at end of the variable scope.
Example:
class MyClass {
public:
~MyClass() noexcept {
// some code to execute immediately after setting params A and B
}
// abstract setting methods
MyClass &SetA() { return *this; }
MyClass &SetB() { return *this; }
};
MyClass SomeFunction() {
return MyClass();
}
int main() {
// Correct usage:
SomeFunction()
.SetA()
.SetB();
// destructor here
// I want to prevent it:
auto my_class = SomeFunction();
my_class
.SetA()
.SetB();
{
auto my_class2 = SomeFunction();
my_class2
.SetA()
.SetB();
// destructor of `my_class2` here
}
// destructor of `my_class` here.
return 0;
}