Vector of objects with different member functions?

Hi all,
I'm writing here because I have a problem with objects with different member functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Model_Class
{
public:
	class Parameter_Class
	{
	public:
		string Name;
		int unsigned ID;
		double Function(double a);
	};
	vector<Parameter_Class> Parameters;
};

int main()
{
	Model_Class Model;
	Model.Parameters.emplace_back();
}:


I would like to define different functions for Model.Parameters[0].Function, Model.Parameters[1].Function, Model.Parameters[2].Function, etc...
For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double Model.Parameters[0].Function(int a)
{
         double b;
         b = a+2;
}

double Model.Parameters[1].Function(int a)
{
         double b;
         b = a*3;
}

double Model.Parameters[2].Function(int a)
{
         double b;
         b = a*100;
}


How can I do?

Thank you very much for the answer,
Last edited on
One way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Model_Class
{
public:
   class Parameter_Class
   {
   public:
      string Name;
      int unsigned ID;
      int (*Function)( int a );
   };
   vector<Parameter_Class> Parameters;
};

int main()
{
   Model_Class Model;
   Model.Parameters.push_back( { "add 2"    , 000, []( int a ){ return 2   + a; } } );
   Model.Parameters.push_back( { "times 3"  , 111, []( int a ){ return 3   * a; } } );
   Model.Parameters.push_back( { "times 100", 222, []( int a ){ return 100 * a; } } );

   int n = 3;
   cout << "Initial: " << n << '\n';

   for ( int i = 0; i < 3; i++ )
   {
       n = Model.Parameters[i].Function( n );
       cout << "After " << Model.Parameters[i].Name << ": " << n << '\n';
   }
}


Initial: 3
After add 2: 5
After times 3: 15
After times 100: 1500
Thank you very much, it's exactly what i was searching for.
Can I ask 2 other things?
If i want ti define the functions with the output type "auto" and with variabile number of inputs how can i do?
Is it possibile to import the function from an external textfile?
Thank you very much!
fedegp wrote:
If i want ti define the functions with the output type "auto"

You can't. A function returns either a variable of a specific type or nothing (void). In some circumstances (see https://en.cppreference.com/w/cpp/language/auto ) the type can be deduced rather than stated explicitly, but it will nevertheless have some type.

If you want functions of different types then you can use template functions, to save having to write one of each type. If you want them to be elements of your class then the class will also have to be a template class.
See http://www.cplusplus.com/doc/tutorial/functions2/



fedegp wrote:
variabile [sic] number of inputs

Either:
- overload the function;
- use default arguments (default values will be used if that argument is not available);
- use variadic templates.


fedegp wrote:
Is it possibile [sic] to import the function from an external textfile?

No idea what you mean. Your terminology, "import", might reflect a previous use of java or python, but it is alien to c++.

You can define the function in a separate .cpp file and link it if you wish. Your code that uses it will need an appropriate included header (.h) file to know the function prototype.
You don't need to use lambda functions as I did in my example; you could just put the names of pre-defined functions there. These functions may be in the same or other .cpp files, but their declarations must be known at the point of use, either by defining them above in the same file, or declaring a function prototype (which could be included in a .h file).



Your requirements are extremely vague. Please be very specific about what exactly you are trying to do, or we may give you completely inappropriate - or non-committal - advice. That may include you giving a specific example, or a precise statement of any assignment.
Last edited on
Is it possibile to import the function from an external textfile?

One of the typical homework assignments is to write "a calculator". You probably know the type: user types "aaa bbb ccc" and the program converts aaa and ccc into numbers, consults a switch for bbb (is it +, -, *, or / ) and then evaluates the result.

Your "functions" are of that level. All your functions are basic operators with one of the operands as constant.

lastchance made the Parameter_Class to contain a function pointer and assigned a lambda closure to that pointer. You don't have to use lambdas. You can use "real" functions:
1
2
3
4
5
6
int foo2( int );

int main()
{
   Model_Class Model;
   Model.Parameters.push_back( { "add 2", 000, foo2 );

You have to parse input and assign/create correct "functions", just like in the calculator.


Is it possible? Yes.
Sorry, I will try to clarify my problem.
I want to create a class of parameters in which the functions can have variable number of parameters. For example Model.Parameters[0].Function can have one input variable and Model.Parameters[1].Function has 2 input variables.
1
2
Model.Parameters[0].Function = []( int a ){ return 2   + a; };
Model.Parameters[1].Function = []( int a, int b ){ return a + b; };


This is impossible in the code reported before because in the definition of Parametes.Function it's imposed only a as input variable.
This thing would be useful for me to run a for cycle and call all the functions without knowing a priori the input variables.
In addition I would have an automatic definition of the output of the function (see code below in the definition of the class).

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Model_Class
{
public:
   class Parameter_Class
   {
   public:
      string Name;
      int unsigned ID;
      auto (*Function)( /* Variable number of input variables */ );
      auto Value;
   };
   vector<Parameter_Class> Parameters;
};

int main()
{
    /* Parameters Deinition */
    Model_Class Model;
    Model.Parameters.push_back( { "Function 0"    , 0, []( /* 1 input variable */ int a ){ return 2   + a; } } );
    Model.Parameters.push_back( { "Function 1"  , 1, []( /* 2 Input variables */ int a, int b ){ return 3   * a + b; } } );

   /* Parameters.Function call */
   for (int unisgned i=0;i<Model.Parameters.size_of(); i = i + 1)
   {
         /* Call of the function contained in the i-th parameter element */
        Model.Parameters[i].Value=Model.Parameters[i].Function(/* The number and type of the input variable change according to the index i. In the first there is one input and in the second 2 for example */);
       }
   return 0;
}



For the second question I did before, I was meaning to input , in some way the command to do in a function.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Model_Class
{
public:
   class Parameter_Class
   {
   public:
      string Name;
      int unsigned ID;
      auto (*Function)( /* Variable number of input variables */ );
      auto Value;
   };
   vector<Parameter_Class> Parameters;
};
int main()
{
      // Line 1: Input command as a string or in some way*/
      Model_Class Model;
      Model.Parameters.push_back( { "Function 0"    , 0, []( int a ){ return Command; } } );
   return 0;
}


Sorry if the question are absurd but I'm not an expert of C++ and I'm trying to understand If I can do it or not. Thank you very much for the support.
fedegp wrote:
I want to create a class of parameters in which the functions can have variable number of parameters.
1
2
      auto (*Function)( /* Variable number of input variables */ );
      auto Value;

No, I don't think there is any way you can do that. "auto" isn't itself a type.


I was meaning to input , in some way the command to do in a function.
[]( int a ){ return Command; }
No, you can't do that, either. C++ doesn't have the equivalent of the evil eval that you have in javascript.


BUT ...


If your functions are simple enough for you to write a parser (@keskiverto's post) then you might be able to kill two birds with one cannonball:
1
2
3
4
5
6
7
   class Parameter_Class
   {
   public:
      string Name;
      int unsigned ID;
      string Command;   // This is the function you are going to parse
   };


For example, if your parser could pick out the operations from the string "2A+B" then this could be your "function" command.


OR ...


If, as might be guessed - because you still haven't told us - most calculator-type operations are either binary functions or unary functions, you could simply store a binary function pointer (it would have to be a double, not auto) and another variable which told you how many operands there were (in the case of a unary function you would simply ignore the second parameter).


OR ...


(This is not really thought through) maybe your functions could be of the form
double f( initializer_list<double> )
since a variable number of parameters could go in the initialiser_list brackets.


Best I can suggest, I'm afraid. Maybe somebody else will come up with better.
Last edited on
Last question:
I was hoping that exists a way to do at least the multiple number of input variabile. Do you know a way to use a variabile number of inouts imposing the same type to all the functions?

Edit: I read now the edited answer.
I will try to study your proposals, thank you very much. Your answers have been very very helpful.
Last edited on
fedegp wrote:
Do you know a way to use a variabile number of inouts imposing the same type to all the functions?


Only by cheating with an initializer_list. All functions have the form
double f( initializer_list<double> )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <initializer_list>
using namespace std;


double sum( initializer_list<double> arguments )
{
   double result = 0;
   for ( auto x : arguments ) result += x;
   return result;
}


double product( initializer_list<double> arguments )
{
   double result = 1;
   for ( auto x : arguments ) result *= x;
   return result;
}


int main()
{
   double a, b, c;
   cout << "Input a b c: ";   cin >> a >> b >> c;

   double (*f)( initializer_list<double> ) = sum;

   cout << "a + b = "     << f( { a, b    } ) << '\n';
   cout << "a + b + c = " << f( { a, b, c } ) << '\n';

   f = product;
   cout << "a * b = "     << f( { a, b    } ) << '\n';
   cout << "a * b * c = " << f( { a, b, c } ) << '\n';
}


Input a b c: 2 3 4
a + b = 5
a + b + c = 9
a * b = 6
a * b * c = 24

Last edited on
Topic archived. No new replies allowed.