Search code examples
c++classstructcallbackboggle

How to call a non-static member function within its own class in C++


I am building a class Boggle in C++. In the Boggle class, I have declared a structure type called boardIndex:

struct Boggle::boardIndex {
    int row, col;
};

And a callback function to compare two "boardIndex"s:

int Boggle::CmpByIndex(boardIndex a, boardIndex b)

I want to pass the callback function to a Set of boardIndex elements within the Boggle.cpp file:

Set<boardIndex> usedIndices(CmpByIndex); 

Is this possible? In the current form, I'll get an error that a "reference to non-static member function must be called." I don't have any objects of the Boggle class - is there another way of calling the CmpByIndex function here?


Solution

  • I don't have any objects of the Boggle class - is there another way of calling the CmpByIndex function here?

    If you do not have any objects then you cannot call non-static member function. So you have 2 solutions:

    • make this function either static or non member of class Boogie
    • create or somehow obtain an instance of Boogle and bind it to call of this method