Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I decrease the number of overloaded functions

I want to know if there is an approach to decrease the number of overloaded function (function edit) in the below code.

 class foo
  {
  public:
      foo(int _a, char _b, float _c) : a(_a), b(_b), c(_c){};
      void edit(int new_a);
      void edit(char new_b);
      void edit(float new_c);
      void edit(int new_a, char new_b);
      void edit(int new_a, float new_c);
      void edit(char new_b, float new_c);
      void edit(int new_a, char new_b, float new_c);
      void info();
  private:
     int   a;
     char  b;
     float c;
  };

Here is the implementation of the edit functions :

void foo::edit(int new_a)
{
    a = new_a;
}
void foo::edit(char new_b)
{
    b = new_b;
}
void foo::edit(float new_c)
{
    c = new_c;
}
void foo::edit(int new_a, char new_b)
{
    a = new_a;
    b = new_b;
}
void foo::edit(int new_a, float new_c)
{
    a = new_a;
    c = new_c;
}
void foo::edit(char new_b, float new_c)
{
    b = new_b;
    c = new_c;
}
void foo::edit(int new_a, char new_b, float new_c)
{
    a = new_a;
    b = new_b;
    c = new_c;
}

The edit function will let the user change the parameters of the object as he wishes. But the thing is that if we add a new parameter we have to add to many overloaded function and I thought there should be a better way.

Here with 3 parameters we need 7 overloaded functions but if we had 4 parameters (a, b, c and d) then we had to develop 14 overloaded function! That's why I think there should be a better approach.

Thanks.

like image 892
meysamimani Avatar asked Aug 31 '25 22:08

meysamimani


2 Answers

With variadic and (ab)using std::get<T> on std::tuple, you might do

template <typename... Ts>
void edit(Ts... values)
{
    ((std::get<Ts&>(std::tie(a, b, c)) = std::get<Ts&>(std::tie(values...))), ...);
}

Demo.

Note: I use std::get<Ts&>(std::tie(values...)) instead of simply values to get error with duplicated input types(edit(42, 42);).

like image 52
Jarod42 Avatar answered Sep 04 '25 15:09

Jarod42


You can avoid the huge number of overloads and still allow the caller to set more than one member in a single expression:

class foo
  {
  public:
      foo(int _a, char _b, float _c) : a(_a), b(_b), c(_c){};
      foo& edit(int new_a) { a = new_a; return *this;}
      foo& edit(char new_b) { b = new_b; return *this; }
      foo& edit(float new_c) { c = new_c; return *this; }
      
  private:
     int   a;
     char  b;
     float c;
};

int main() {
    foo f(1,'c',2.0);
    f.edit(42).edit(42.0f).edit('a');
}

Adding a member requires you to write one overload rather than N to support all combinations.

like image 33
463035818_is_not_a_number Avatar answered Sep 04 '25 16:09

463035818_is_not_a_number