Instantiating object with function pointer as paramater

Basically I have class B, that in the constructor is creating two class A objects. One class A object is being sent a pointer to the function lessThan, and other is sent a pointer to gearterThan.

I have tried lots of different approaches, but am not understanding where I am going wrong with function pointers.

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
template <typename T> class A	
{
public:

	// A constructor
	A(int capacity, bool(*compPtr)(T));

	bool(*m_compPtr)(T);
};

template <typename T> class B
{
public:

	A<T> m_one = new A<T>;
	A<T> m_two = new A<T>;

	//B constructor
	B();

	// Less than operator
	bool lessThan(T left, T right);

	// greater than operator
	bool greaterThan(T left, T right);

	//bool(*lessThanPtr)(T) = &lessThan;
	//bool(*greaterThanPtr)(T) = &greaterThan;

	bool(*lessThanPtr)(T);
	bool(*greaterThanPtr)(T);

};



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

//A constructor
template <typename T>
A<T>::A(int capacity, bool(*compPtr)(T)) {
	m_compPtr = compPtr;
}



template <typename T>
B<T>::B(int capacity) {
	// Make two objects
	A <T> m_one(capacity, lessThanPtr);
	A <T> m_two(capacity, greaterThanPtr);
}

// Less than
template <typename T>
bool B<T>::lessThan(T left, T right) {
	if (left > right) {
		return true;
	}
	else {
		return false;
	}
}

// greater than operator
template <typename T>
bool B<T>::greaterThan(T left, T right) {
	if (right > left) {
		return true;
	}
	else {
		return false;
	}
}
Last edited on
First, an outline of the approach below:
1. have a template baseclass that overloads operators < and > and then define the lessThan() and greaterThan() template functions with this base class objects

2. declare a type for a function pointer that takes 2 objects of the template base class and returns bool through the using declaration (see below)

3. then proceed to declare custom template types A and B as in your example (though I have a feeling that with the above background we could move to B directly without going through A)

here's the code though you might have to tinker with it a bit to make it fit your exact specifications that have not been sent through:
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
#include <iostream>
#include <string>

template <typename T>
struct baseClass
{
    T m_data;
    std::string m_name;
    baseClass (const T data, const std::string& name)
        : m_data(data), m_name(name){}
    bool operator < (const baseClass<T>& rhs)
    {
        return *this.m_name < rhs.m_name;
    }
    bool operator > (const baseClass<T>& rhs)
    {
        return *this.m_name > rhs.m_name;
    }
};
template <typename T>
bool lessThan (const baseClass<T>& lhs, const baseClass<T>& rhs)
{
    return lhs < rhs;//operator < needs to be overloaded for baseClass<T>
}
template <typename T>
bool greaterThan (const baseClass<T>& lhs, const baseClass<T>& rhs)
{
    return lhs > rhs;//operator > needs to be overloaded for baseClass <T>
}

template <typename T>
using typeCompareFuncPtr = bool(*)(T, T);
//using declaration for function pointers:
http://stackoverflow.com/questions/16498969/how-do-i-typedef-a-function-pointer-with-the-c11-using-syntax

template <typename T>
struct A
{
    int m_capacity;
    typeCompareFuncPtr<T> m_ptr;
    A(const int capacity, const typeCompareFuncPtr<T> ptr)
        : m_capacity(capacity), m_ptr(ptr){}
};

template <typename T>
struct B
{
    A<T> m_lessA = A<T>(0, lessThan);
    A<T> m_greaterA = A<T>(0, greaterThan);
};

int main()
{

}
Last edited on
Topic archived. No new replies allowed.