0

I am a newbie trying to code a Menu class and I want it to be able to call functions that are part of other classes. I have sought thoroughly on the internet, but the answers that I've found doesn't fit my needs.

Here is a straightforward piece of code that defines my problem:

#include <iostream>

class Class1;//forward declaration
typedef void (Class1::*FunctionPtr_t)();//type: Pointer to a function in Class1 scope

class Class1
{
public:
    void function1()
    {std::cout << "function1 executed!";}//Test function to execute from Class2
};

class Class2
{    
public:
    FunctionPtr_t myfcnptr = NULL;//Pointer to a function in Class1
};

int main()
{
  Class2 myclass2;

  myclass2.myfcnptr = &Class1::function1;//Assign function to pointer (Everything OK here)
  (myclass2.*myfcnptr)();//Compilation Error: 'myfcnptr' was not declared in this scope
}

I have messed around, made both classes friend and used scope operators almost everywhere. I know the solution must be quite easy and that I'm making a ridiculous mistake, but I don't catch it! Thank you for your patience ;)

8
  • 1
    you need an object of Class1 to call a member function of that class. Do you really need to mess around with function pointers? Commented Jun 3, 2016 at 13:17
  • you should explain better your original problem and what you want to achieve. This looks like a xy problem because it is not clear why you need function pointers and the code you posted makes (no offense) little sense to me Commented Jun 3, 2016 at 13:20
  • no offense intended ;). I am trying to create a menu for an Arduino project and each menu option triggers a function. The menu class is generic so it has to be capable of manage a generic array of functions, which will ultimately be specified in the construction process. However some of the functions belong to other classes. As an example, the menu access a database which is managed by a database class, though my menu class will be passed a reference to a function (for example readData) and has to be able to call it. Commented Jun 4, 2016 at 10:41
  • Are there any workaround to deal with this kind of situation? Please remember that as I am programming for an Arduino, I can't implement the Standard Library. Commented Jun 4, 2016 at 10:41
  • you would not need to implement the standard library, others did that already for you :P (unfortunately not available for arduino). What is "the situation"? What do you actually need? Calling a (non-static) member function without an object does not make sense in the first place. Commented Jun 4, 2016 at 10:44

4 Answers 4

1

The correct syntax would be (myclass2.*myclass2.myfcnptr)();.

But in your case, Class1 and Class2 are unrelated, so Class2 cannot sensibly call a member function from Class1. You need an instance of Class1, or something derived from Class1, to perform that call.

Sign up to request clarification or add additional context in comments.

2 Comments

SOLVED->Quite near!!! The correct syntax is (myclass1.*myclass2.myfcnptr)(); , where myclass1 is an instantiation of Class1. I'm sure you meant that, but anyway you pushed me in the right way ;) Thank you!!
@Leo I've split my answer between syntax and semantics, but maybe that makes it unclear...
1

Ultimately, you will need to create a relationship between the two classes Class1 and Class2 by utilizing C++'s class inheritance system; as previously mentioned, Class2 simply cannot call a Class1 member function without having a relationship. You could, perhaps, cause Class2 to extend Class1 by writing something like this on the first line of your class definition:

class Class2 : public Class1
{  // ...

You would also want to mark the original function in the parent class Class1 as virtual and then provide a definition of the function in the child class Class2.

A good resource to learn about C++'s class inheritance, polymorphism, the virtual keyword, etc., would be http://www.cplusplus.com/doc/tutorial/polymorphism/.

Comments

1

If no instantiation of Class1 is desired, it is also possible to make the method function1 static and call it directly from a Class2 instance. The following code will compile:

#include <iostream>


typedef void (*FunctionPtr_t)();//PAY ATTENTION TO THE NEW typedef !

class Class1
{
public:
    static void function1() //STATIC METHOD
    {std::cout << "function1 executed!";}//Test function to execute from Class2
};

class Class2
{    
public:
    FunctionPtr_t myfcnptr = NULL;//Pointer to a function in Class1
};

int main()
{
  Class2 myclass2;

  myclass2.myfcnptr = &Class1::function1;//Assign function to pointer
  (*(myclass2.myfcnptr))();
}

Comments

0

You are trying to call a member function via a member function pointer, but you are not specifying the instance of Class1 to call against.

For example:

FunctionPtr_t func = &Class1::function1;
Class1 instance;
(instance.*func)();

Notice that I've had to specify the instance to call against. The pointer just references a function. This is different to languages like C# where you can reference a function and that reference also tracks the object containing the function.

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.