0

I have currently defined a function pointer and to me it seems like the function matches the definition, however I am getting an error:

1 IntelliSense: a value of type "std::string (RSSCrawler::)(const web::json::value &headlines)" cannot be assigned to an entity of type "std::string ()(const web::json::value &headlines)"

I am not sure what is wrong, but here is my code

string(*GetHeadline)(const value&headlines);
GetHeadline = Extract;


string RSSCrawler::Extract(const value &headlines)
{
    return "";
}
9
  • 3
    Plain function pointers are not member function pointers. Research the latter. Commented Jul 16, 2016 at 10:13
  • 1
    Extract is a member function. GetHeadline is a non-member function pointer. They are not the same type. Commented Jul 16, 2016 at 10:14
  • it should work if Exrtact is a static function. And you can assign function pointer using GetHeadLine = RSSCrawler::Extract; Commented Jul 16, 2016 at 12:27
  • @CODError but the compiler message shows clearly that it's not a static member function, and that's the problem. Coincidentally, I edited a note about static member functions into my answer just 1 minute before you commented. :P Anyway, making it a static method has the same issues as capturing the pointer-to-member in a lambda, in that it complicates things by meaning we must then also provide an instance to work with. As said in my answer to the OP's next question (in Linked) - it's much easier to have the lambda capture this and use it to act 'on behalf of' an instance. Commented Jul 16, 2016 at 12:37
  • 1
    @underscore_d Yes, I agree static methods add some complications when you want current object (this) inside your function. But its perfectly fine to use static methods when it doesn't want your current object (this), right? I guess in the example code it doesn't use current object inside method body :P . Static method wins in this case ;) yes? <its just for my own understanding that I am arguing with you> Commented Jul 16, 2016 at 13:08

1 Answer 1

2

The compiler explained this with a type mismatch error and showing the difference in the first set of parentheses. You need a member function pointer. That is a separate type from a 'plain'/free function pointer. (static member functions act like free functions in this sense, but that's not what you have.)

You can find plenty tutorials about these, but here's a quick reference. (I have to restrain myself not to de-capitalise these function and variable names because it just looks wrong, even without SO's auto-formatting.)

// Declare pointer-to-member-function of a given class and signature
std::string (RssCrawler::* GetHeadline)(const value&);
// Bind it to any method of the same class and signature
GetHeadline = &RssCrawler::Extract;
// Call it on a given instance of said class
std::cout << (someInstance.*GetHeadline)(someValue) << std::endl; // operator .*

Or you can do this to get a const initialised pointer, though I think that defeats the purpose of a function pointer, except for const-correctness when declaring them as arguments to other functions...

std::string (RssCrawler::*const GetHeadline)(const value&) {
    &RssCrawler::Extract
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.