can not create a function pointer of class

Pages: 123
Hi Galik,i have one problem.You may remember the problem we were discussing here. I am pasting the code again here as you have written it.I have changed it a bit in get_example() function.
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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>

template<typename Data_T>
class test
{
public:
	void test_func_1(const Data_T &var)
	{
		std::cout << "test_func_1: " << var << std::endl;
	}
};

template<typename Data_T>
struct example
{
	int a;
	float b;
	void (test<Data_T>::*fun_ptr)(const Data_T& a);
};

template<typename Data_T>
std::vector<example<Data_T> > get_example(const Data_T&, size_t i)
{
      std::vector<example<Data_T> > test_vect;
	static example<Data_T> example_array[] =
	{
		{1, 2, &test<Data_T>::test_func_1}
	};

for(int i=0;i<size;i++)
		test_vect.push_back(example_array[i]);
	return test_vect;

}

int main()
{
	int i = 3;
	double d = 23.78;

	test<int> a;
	test<double> b;

	(a.*get_example(int(), 0).fun_ptr)(i);
	(b.*get_example(double(), 0).fun_ptr)(d);
}


Now how can i access the fuc_ptr()?

i am doing (test_vect.*fun_ptr)(i);. But it gives error.

Last edited on
Oh my goodness. I thought you'd forgotten about this crazy scheme ;o)

Well you changed the get_example() function to return a vector of examples rather than just examples. So you need to dereference each element of that vector to get to your 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <cstdlib>
#include <vector>

template<typename Data_T>
class test
{
public:
	void test_func_1(const Data_T &var)
	{
		std::cout << "test_func_1: " << var << std::endl;
	}
};

template<typename Data_T>
struct example
{
	int a;
	float b;
	void (test<Data_T>::*fun_ptr)(const Data_T& a);
};

template<typename Data_T>
std::vector<example<Data_T> > get_example(const Data_T&)
{
	std::vector<example<Data_T> > test_vect;
	static example<Data_T> example_array[] =
	{
		{1, 2.9, &test<Data_T>::test_func_1}
		, {3, 4.7, &test<Data_T>::test_func_1}
	};

	int size = sizeof(example_array)/sizeof(example_array[0]);

	for(int i = 0; i < size; i++)
	{
		test_vect.push_back(example_array[i]);
	}
	return test_vect;

}

int main()
{
	int i = 3;
	double d = 23.78;

	test<int> a;
	test<double> b;

	std::vector<example<int> > test_vec_i = get_example(int());

	for(size_t n = 0; n < test_vec_i.size(); ++n)
	{
		(a.*test_vec_i[n].fun_ptr)(i);
	}

	std::vector<example<double> > test_vec_d = get_example(double());

	for(size_t n = 0; n < test_vec_i.size(); ++n)
	{
		(b.*test_vec_d[n].fun_ptr)(i);
	}
}


But why you would ever use such an elaborate scheme is still kind of beyond me...
No it stills there ;)
I am modifying it a bit further to replicate exactly what i am facing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<typename Data_T>
class test
{
public:
	void test_func_1(const Data_T &var)
	{
		std::cout << "test_func_1: " << var << std::endl;
	}
    void do_stuff()
    {
    std::vector<example<int> > test_vec_i = get_example(int());
//now here i want to access the test_func_1 function,means i want to execute it here,not directly calling it but through the vector
   }
};
int main()
{
test<int> a;
a.do_stuff();
}

Last edited on
There has to be a better 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstdlib>
#include <vector>
template<typename Data_T>
class test;

template<typename Data_T>
struct example
{
	int a;
	float b;
	void (test<Data_T>::*fun_ptr)(const Data_T& a);
};

template<typename Data_T>
std::vector<example<Data_T> > get_example(const Data_T&);

template<typename Data_T>
class test
{
public:
	void test_func_1(const Data_T &var)
	{
		std::cout << "test_func_1: " << var << std::endl;
	}

    void do_stuff()
	{
		std::vector<example<Data_T> > test_vec = get_example(Data_T());

		for(size_t i = 0; i < test_vec.size(); ++i)
		{
			(this->*test_vec[i].fun_ptr)(i);
		}
	}
};

template<typename Data_T>
std::vector<example<Data_T> > get_example(const Data_T&)
{
	std::vector<example<Data_T> > test_vect;
	static example<Data_T> example_array[] =
	{
		{1, 2.9, &test<Data_T>::test_func_1}
		, {3, 4.7, &test<Data_T>::test_func_1}
	};

	int size = sizeof(example_array)/sizeof(example_array[0]);

	for(int i = 0; i < size; i++)
	{
		test_vect.push_back(example_array[i]);
	}
	return test_vect;

}

int main()
{
	test<int> a;
	a.do_stuff();

	test<double> b;
	b.do_stuff();
}
Would you mind saying again what exactly you want to do? I read the previous posts but didn't quite understand... :/ It would be great if you posted some pseudo-code showing how you would like things to work ideally.
Galik,is there any way to declare the test_func_1 as static. Because if i do so compiler gives me an error in the following definition
1
2
3
4
5
static example<Data_T> example_array[] =
	{
		{1, 2.9, &test<Data_T>::test_func_1}
		, {3, 4.7, &test<Data_T>::test_func_1}
	};


because i wan to call the *test_vec[i].fun_ptr)(i) without this pointer. I don't want to make the "test_func_1" part of every object.
Last edited on
You are trying to call a member function. You can only do that from an object. So either it has to be from this-> or it has to be from a variable of the right class.
Galik,i got the point . actually i wanted to declare these functions as static so that ot to make all these functions to make part of every object. But the isseu is that if i make these static i have to make lots of member variables static which doesn't make sense.
Last edited on
If the functions need to access the member variables then there is no benefit to making them static because you will still have to pass in an object as a parameter.
Topic archived. No new replies allowed.
Pages: 123