0

Hi guys i have been trying to implement a higher order member function that takes a method that returns a boolean value. The problem i have is that because i am using a template class i am unsure how to call my method in main. I know that normally you pass the address into the function call but it is not working with classes.

Would appreciate it if anyone could help! Thanks

Dictionary.h

using Comparison = auto (Key)->bool;

bool removeIf(Comparison);

template<class Key, class Item>
bool Dictionary<Key, Item>::removeIf(Comparison compare)
{

    if (compare == true)
    {
        std::cout << "Absent" << std::endl;

    }
    else
    {
        std::cout << "Present" << std::endl;
    }

}

main.cpp

Dictionary<std::string, std::string> dict;

std::cout <<"---Dict---" << std::endl;
dict.displayDictionary();

bool (*f)(std::string) = dict.remove;

f("1");
efe
  • 11
  • 3
  • 1
    A non-static member function can not be converted to a plain function pointer. A non-static member function needs an object to be called on, and a plain function pointer doesn't have any information about that. Today with [lambdas](https://en.cppreference.com/w/cpp/language/lambda), [`std::bind`](https://en.cppreference.com/w/cpp/utility/functional/bind) and [`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function) one seldom need function pointers at all. – Some programmer dude Dec 19 '19 at 15:24
  • `but it is not working with classes` "is not working" is unclear. How does it not work? Could you point out the part in your example where you attempt to pass address to the function? – eerorika Dec 19 '19 at 15:25

0 Answers0