Reputation: 75
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 ;)
Upvotes: 0
Views: 2868
Reputation: 75
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))();
}
Upvotes: 1
Reputation: 48
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/.
Upvotes: 0
Reputation: 62472
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.
Upvotes: 0
Reputation: 63114
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.
Upvotes: 1