Diffirent methods in the class trough pointer

Hallo.
I will make the class with diffirent methods. I make this by this manual http://alenacpp.blogspot.ru/2007/04/blog-post.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class My_class
{
public:
  void (*fn)();
}

void fn_1() {}

void fn_2() {}

map<string, void(*)()> my_map = {{"fn_1", fn_1}, {"fn_2", fn_2}};

My_class obj_1, obj_2;
obj_1.fn = my_map[0];
obj_2.fn = my_map[1];

Now I will make functions with diffirent parameters:
1
2
void fn_1(My_type_1*) {}
void fn_2(int*, My_type_2*) {}


I try this trough template:
1
2
3
4
5
class My_class
{
  template<class T>
  void (*fn)();
}

but fail:
data member ‘fn’ cannot be a member template
Thank for any ideas.
Last edited on
1) Your example code presents so many errors that doesn’t even compile.
You should consider asking for help about minimal examples which reproduce your issue.
Working example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <map>
#include <string>


class My_class
{
public:
    void (*fn)();
};

void fn_1() {}
void fn_2() {}


int main()
{
    std::map<std::string, void(*)()> my_map = {{"fn_1", fn_1}, {"fn_2", fn_2}};

    My_class obj_1, obj_2;
    obj_1.fn = my_map["fn_1"];
    obj_2.fn = my_map["fn_2"];
    return 0;
}


2)
I try this trough template:
As far as I know, you can’t have a pointer to a function template:
https://stackoverflow.com/questions/4573941/c-function-pointer-to-the-template-function-pointer

Thank for correction. I see yet the template is not good in my case.
Is other way?
Is other way?

To do what?

It’s hard to guess what’s the point in having a container which stores objects which store a pointer to a template function.

I think usually the more a class is general, the better. One of the strongest point of OOP is the code is easy to reuse. When possible, a class should be conceived as if it could become part of a separate library. That’s possible if there are very few ‘connections’ between the class and the rest of the code, so that everything which refers to the class is inside its header and source file.

If you want your class to depend from functions that are outside it, you make it less general and harder to reuse.
That’s quite common, anyway, but it’s usually achieved by friendship.

So, if yours it’s an academic question, my impression is you could try to substitute overloaded friend functions for pointer to functions or write your functions where they are needed by a lambda.
Otherwise, if you are trying to develop a ‘real’ code, I’m afraid we need a “Minimal, Complete, and Verifiable example” of your problem
https://stackoverflow.com/help/mcve
along with a description of what your program should do.
Now I will make functions with diffirent parameters:
1
2
void fn_1(My_type_1*) {}
void fn_2(int*, My_type_2*) {}

How would you call such a thing?

When you call a function, the number and type of parameters are specified at compile time. So even if you could do:
1
2
3
4
5
6
iMyClass obj;
f (condition) {
    obj.fn = fn_1;
else {
    obj.fn = fn_2;
}

there would still be no way to call it:
1
2
3
4
5
My_type_1 a;
My_type_2 b;
...
obj.fn(a); // error if obj.fn == fn_2
obj.fn(b); // error if obj.fn == fn_1 


If you can bind the object to the function at compile time then a template would work:
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
#include <iostream>

template <class T>
class MyClass {
public:
    MyClass(void (*f)(T*)) :
	fn(f)
    {}
    void (*fn)(T*);
};


void printInt(int *i) { std::cout << *i << '\n'; }
void printDouble(double *d) { std::cout << *d << '\n'; }

int
main()
{
    MyClass<double> f1(printDouble);
    MyClass<int> f2(printInt);

    int i {3};
    double d {2.7};
    f1.fn(&d);
    f2.fn(&i);
}



Topic archived. No new replies allowed.