1

I have the following struct:

template <typename T> struct avl_tree {
    T data;
    int balance;
    struct avl_tree <T> *Link[2];
    int (*comp)(T, T);
};

What I would like to do is to point the comp function pointer to a valid function at runtime, then have all instances of struct avl_tree<T> to be able to access this function.

int compare(int a, int b) {
    return ( a - b );
}

Is this possible to do so that I can do something like:

avl_tree<int> tree(new avl_tree<int>);
tree = insert(tree, 9);
std::cout << tree->comp(tree->data, 9) << '\n';//Should print 0

Finally got the answer to this. Solution:

in struct avl_tree:

typedef int (*compare)(T, T);
static compare comp;

above main:

template <typename T> int (*avl_tree<T>::comp)(T, T);//Initialise the pointer

in main:

avl_tree<int>::comp = compare;//Set the static member function pointer to the function to use

In response to my previous question, here is how you can use it:

avl_tree<int> tree(new avl_tree<int>);
tree = insert(tree, 9);
std::cout << avl_tree<int>::comp(tree->data, 9) << '\n';//Should print 0

Simple :D

smac89
  • 39,374
  • 15
  • 132
  • 179

3 Answers3

1

Im having some difficult to understand your question, but Im thinking why dont you do simply something like that:

template <typename T> struct avl_tree {
    T data;
    int balance;
    struct avl_tree <T> *Link[2];
    int comp(T x) { return compare(data, x);}
};

and make sure that your T struct or type has a conversion method to int.

Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • Not what I am looking for but thanks for the solution. @dwarduk gave me the correct name for what I need which is to make the function pointer static, and use it the way Barmer has shown below – smac89 Sep 03 '13 at 20:40
1

Declare comp static:

template <typename T> struct avl_tree {
    T data;
    int balance;
    struct avl_tree <T> *Link[2];
    static int (*comp)(T, T);
};
template <typename T> int(*::comp)(<T>, <T>);

You can later assign it for a particular template instance with:

avl_tree<int>::comp = int_compare;

See this SO question and this outside site for more information about initialization of static members of template classes.

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Can you please explain this part: `template int(*::comp)(, );` ? – smac89 Sep 03 '13 at 20:38
  • This method still produces compiler issues – smac89 Sep 03 '13 at 21:05
  • I may not have the syntax quite right. The examples I extrapolated from were for data members, not function members. Declaring function pointers is tricky (that's why people often typedef them). – Barmar Sep 03 '13 at 21:16
0

Do like following

avl_tree<int> *tree =new avl_tree<int>;

tree->comp =compare; //Your function
std::cout << tree->comp(tree->data, 9) << '\n';

See here

P0W
  • 46,614
  • 9
  • 72
  • 119
  • But would this work for all instances of the class `avl_tree`? i.e. I would need to do this for all instances of the class but I can't due to encapsulation of the class from the comparison function. Which is why I want to do it just once and all instances will use that declaration – smac89 Sep 03 '13 at 19:58
  • @Smac89 are you saying want every instantiation of avl_tree? to use the same `compare` function? – Ryan Haining Sep 03 '13 at 20:11
  • 1
    I think he means all `avl_tree` should use one comparison function, all `avl_tree` should use a different one, and so on. – Barmar Sep 03 '13 at 20:15
  • @RyanHaining yes that is that I am saying -Every instance with the same type should use the same compare – smac89 Sep 03 '13 at 20:16
  • @Smac89 in that case you want Tomas Badan's solution. Though there are more polymorphic ways to do this, such as `std::less` – Ryan Haining Sep 03 '13 at 20:22
  • @Smac89 for every unique `T` you can have separate `comp`. All child of same `T` can use same `comp`. If fact, I guess, you'll be using `comp` through `*tree`, so it will make sense to attach all childs to use same `comp` – P0W Sep 03 '13 at 20:24