0

I'm trying to create a function pointer. My code:

Header file:

#pragma once
#include <stdio.h>

class my_class
{
private:
    int function(int x);
    int *(*foo)(int);

public:
    my_class();
    ~my_class();
};

css file:

#include "my_class.h"

int my_class::function(int x) {
    return 1;
}

my_class::my_class() {
    foo = &function;
}

my_class::~my_class()
{
}

But the line inside my_class::my_class() gives this error:

error C2276: '&': illegal operation on bound member function expression

Putting the mouse cursor over the = in said line makes the following tooltip appear:

a value of type "int (my_class::*)(int x)" cannot be assigned to an entity of type "int *(*)int"

How can I get it to work?

user4581301
  • 33,082
  • 7
  • 33
  • 54
Redz
  • 103
  • 1
  • 1
  • 6
  • Type of `&function` (`&my_class::function`) is `int (my_class::*)(int)` whereas `foo` expects `int* (*)(int)`. – Jarod42 May 23 '18 at 01:26
  • `int *(*foo)(int);` has no hidden `this` parameter, so it cannot accept a member function. Lots of information on that and how to fix it here: https://isocpp.org/wiki/faq/pointers-to-members – user4581301 May 23 '18 at 01:27
  • `function` isn't just a function; it's a *member* function. They're types are *not* synonymous, and thus your assignment is considered invalid. The error message amplifies this substantially. Either move `function` out of `my_class`, or change `foo` to point to the correct type (and prepare yourself to learn how to invoke member functions via a member function pointer and a `this`). – WhozCraig May 23 '18 at 01:29
  • You have to fix type of `foo` or change `function` to be `static` (and change return type). – Jarod42 May 23 '18 at 01:30
  • 1
    `#include ` .... why? – Eljay May 23 '18 at 01:31
  • Even after reading your comments, I'm not sure how to fix it. I'm not very experienced on pointers. I based my implementation on this page, whose code is pretty much the same as far as I can tell: https://www.cprogramming.com/tutorial/function-pointers.html As for , ignore it. It's unrelated to this. – Redz May 23 '18 at 01:42
  • Redz, that link covers how to do it with a plain old free function and then covers the concept in C++ with a function wrapped in a class. It does not cover functions that are class members. Member pointers are not for the faint at heart. Jarod42 told you exactly what you need to do in the first comment and you could not recognize it. This suggest you need to do a bit more reading. The page I linked above (again: https://isocpp.org/wiki/faq/pointers-to-members) goes through it in detail. Read it. Work through the examples. Come back with a narrower problem if you have questions. – user4581301 May 23 '18 at 02:50

1 Answers1

-1

What you think the compiler will do for you is an implicit type cast from a pointer to a member function to a pointer to a normal function. But this is not what happens.

A pointer to a member function is usually not used, because the instance of an object is what is assigned a certain amount of memory and thereby a pointer to a member function might not exactly point to the function and might lead to erroneous results.

You can fix it in two ways:

  1. Define your member function as a static function. Static member functions are not attached to any particular object and can be used without the scope resolution operator.
  2. Another probable fix you can use is to use the this pointer to create a pointer to a member function.
  • Downvoted because this answer talks about the problem but doesn't address the problem in a way that is likely to help the asker. – user4581301 May 23 '18 at 02:52
  • @user4581301 : I have stated a few fixes in the answer that can likely solve the askers problem. I am new here and am not very sure how to reply to posts. Any further clarifications would be very helpful – ganesh prasanna May 23 '18 at 03:02