Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ static pointer to function

I would like to have a private static pointer to a function in my class. Basically, it would look like this:

//file.h
class X {
private:
    static int (*staticFunc)(const X&);
    ...
public:
    void f();

};


//file.cpp
void X::f()
{
    staticFunc(*this);
}

This gives me an "unresolved external symbol" error. I know that static members must be initialized in the .cpp too, I've tried this:

int (X::*staticFunc)(const X&) = NULL;

but this gives me an "initializing a function" error. It gives me an uglier error if I try to initialize it with an existing function. Without "= NULL", I get the same error.

Thanks.

like image 566
stonecup64 Avatar asked Apr 08 '26 08:04

stonecup64


1 Answers

//file.cpp  
int (*X::staticFunc)(const X&);

void X::f()  
{  
staticFunc(*this);  
}
like image 142
TonyK Avatar answered Apr 09 '26 21:04

TonyK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!