How to call member function through member function pointer? [duplicate] - c++

This question already has answers here:
How do I call a pointer-to-member-function?
(4 answers)
Closed 9 years ago.
I want to call member-functions through member-function-pointers. The calling function is also a member.
class A;
typedef int (A::*memFun)();
class A
{
int P(){return 1;}
int Q(){return 2;}
int R(){return 3;}
int Z(memFun f1, memFun f2)
{
return f1() + f2(); //HERE
}
public:
int run();
};
int A::run()
{
return Z(P, Q);
}
int main()
{
A a;
cout << a.run() << endl;
}
I am not doing it correctly, and am getting error-
main.cpp:15:19: error: must use '.*' or '->*' to call pointer-to-member function in 'f1 (...)', e.g. '(... ->* f1) (...)'
return f1() + f2(); //HERE
Please show the correct way to do it.
EDIT - there is another error, which is solved by having-
return Z(&A::P, &A::Q);

(this->*f1)() + (this->*f2)();
Regardless of whether you're calling it from inside the class, you have to explicitly specify the object on which to call (in this case this). Also note the required parentheses. The following is wrong:
this->*f1() + this->*f2()

Like this:
(this->*f1)() + (this->*f2)()

While you don't show it, you most likely have errors when calling Z as well.
You need to call the function like this:
Z(&A::P, &A::Q)

you have to use this
(this->*f1)();

Related

Issue with vector of member function pointers within a struct [duplicate]

This question already has answers here:
C++ Call Pointer To Member Function
(4 answers)
Function pointer to member function
(8 answers)
How to call through a member function pointer?
(2 answers)
Calling a function pointer on a member function [duplicate]
(1 answer)
Closed 17 days ago.
So I have a vector of a struct object containing a function pointer to an external class.
//Menu.h
#include "Byte.h"
struct menuItem
{
Byte (Byte::*funcPoint)(Byte&);
string descript;
};
class Menu
{
private:
vector<menuItem> menuVect;
void runSelection(Byte&, Byte&);
public:
Menu();
void addMenu(string Description, Byte (Byte::*f)(Byte&));
void runMenu(Byte&, Byte&);
void waitKey();
};
I can assign new instances of function pointers as seen here:
void Menu::addMenu(string Description, Byte(Byte::*f)(Byte&))
{
menuVect.push_back({f, Description});
}
But when it comes time execute any given function within the vector, I get an error
void Menu::runSelection(Byte& bX, Byte& bY)
{
int select;
cin >> select;
if (select > 0 && select < menuVect.size() + 1) {
cout << bX.toInt();
menuVect.at(select - 1).funcPoint();
}
else
exit(0);
waitKey();
}
Not sure if its necessary, but I'll include main as well, as one of the function pointers being assigned to the vector
Main
#include "Menu.h"
int main()
{
Menu m;
Byte b1(7);
Byte b2(3);
m.addMenu("1. Func 1", Byte::add);
m.addMenu("2. Func 2", Byte::sub);
m.addMenu("3. Func 3", Byte::mul);
m.addMenu("4. Func 4", Byte::div);
m.runMenu(b1, b2);
return 0;
}
Byte Byte::add(int val)
{
Byte b(this->toInt() + val);
return b;
}
I have already found an obvious work around, by simply not having the functions within an object
The particular errors I'm getting are:
Error (active) E0109 expression preceding parentheses of apparent call must have (pointer-to-) function type
(field)std::vector Menu::menuVect
Error C3867 'Byte::add': non-standard syntax; use '&' to create a pointer to member
But nothing I find when I look this up helps.

Why does calling a functor with an undeclared variable work? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
class foo {
public:
bool operator () (int & i) {
return true;
}
};
int main() {
foo(WhyDoesThisCompile);
return 0;
}
When passing WhyDoesThisCompile (without spaces) to the functor, the program compiles.
Why is this? I tested it on clang 4.0.0.
You are not invoking the functor.
You are declaring a foo, called WhyDoesThisCompile.
Yes, despite the parentheses.
I guess you meant this:
foo()(WhyDoesThisCompile);
// ^^^^^
// temp ^^^^^^^^^^^^^^^^^^^^
// of invocation of op()
// type
// `foo`
… which doesn't.

How to call a pointer to a member function which is retrieved from a map? [duplicate]

This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 5 years ago.
I'm trying to call a pointer to a member function, which I retrieve from a map. The call th.fpObjHandler(md, evd, tokenIdx) seems erroneous, I've tried various different syntaxes (e.g. .* and ->*), but I cannot seem to get it right. Hopefully somebody here is able to help.
struct TokenHandler
{
int tokenIdx;
eventDataStatus_t (Schema::*fpObjHandler)(MessageData &md, EventDataImpl &evd, int &tokenIdx);
};
Schema::event(MessageData &md, EventDataImpl &evd)
{
int tokenIdx = 0;
std::map<std::string, TokenHandler> tokenHandlerMap;
tokenHandlerMap["timeInterval"] = { -1, &Schema::timeInterval };
// ..
// ....
TokenHandler th = tokenHandlerMap.at(key);
if (th.fpObjHandler != NULL) {
th.fpObjHandler(md, evd, tokenIdx); // THIS RESULTS IN ERROR
//..
//...
}
}
eventDataStatus_t Schema::timeInterval(MessageData &md, EventDataImpl &evd, int &tokenIdx)
{
//..
//...
return OK;
}
Schema.cpp:111:54: error: must use ‘.’ or ‘->’ to call
pointer-to-member function in ‘th.TokenHandler::fpObjHandler (...)’,
e.g. ‘(... ->* th.TokenHandler::fpObjHandler) (...)’
th.fpObjHandler(md, evd, tokenIdx);
^
First you need an instance of the class Schema, then use .* or ->*, something like:
Schema schema;
(schema.*th.fpObjHandler)(md, evd, tokenIdx);
Or, as you are already in a method of Schema:
(this->*th.fpObjHandler)(md, evd, tokenIdx);

C++ - using return when the function being called on has parameters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I have a function like this:
void functiont(int a, int b)
{
if(playingnumber=="T")
{
returntransfer=1;
struct A
{
~A() // Destructor to run after returning variable
{
void cardselecth(int playingcolorh, int playingnumberh);
}
}
return returntransfer;
}
}
Anyway I need to grab that returned variable without calling on int a or int b. So in another function I write
newvar=functiont(int a, int b);
And it gives a compile error. I don't know how else to do this. I could write functiont(a,b); and I get an error; I write functiont(int,int); and get an error. I've tried writing functiont(); but then it assumes the function's in the same file and I haven't defined it, which it isn't (I'm transferring across files here, so I need to define any parameters so it knows to refer to another file).
I'm not sure what you want to do there, but at first this function is a void function so it can only return but not return a value. So your signature, respectively the void must be changed according to the type you want to return.
In this case
int functiont(int a, int b)
would be appropriate.
When calling the function you have to pass it two arguments of type integer.
newvar = function(1, 2);
It's a good idea to post the error, too. Best regards
If you prefer to return an int, return type of your function should be int not void. I corrected the code as follows. Note the changes with comment
int functiont(int a, int b)
{
int returntransfer=0; //change , declaration out of if block
// to maintain the scope of variable
if(playingnumber=="T")
{
returntransfer =1;
}
return returntransfer;//change
}
You can declare a struct variable inside a function, but it don't mean just by declaration it will be get called.
struct A
{
~A() // Destructor to run after returning variable
{
void cardselecth(int playingcolorh, int playingnumberh);
}
};//change added ; at end of declaration.
EDIT
Adding complete program for answer to further questions:
#include<iostream>
#include<string>
int functiont(int a, int b)
{
int returntransfer=0; //change , declaration out of if block
// to maintain the scope of variable
std::string playingnumber ="T"; //Added again for completness
if(playingnumber=="T")
{
returntransfer =1;
}
struct A
{
~A() // Destructor to run after returning variable
{
void cardselecth(int playingcolorh, int playingnumberh);
}
};//change added ; at end of declaration.
return returntransfer;//change
}
int main()
{
std::cout<<functiont(2,3);
}

C++: member call to non-static member function pointer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ member-function pointer
How to invoke pointer to member function when it's a class data member?
I've only recently started using C++, so I apologize if the following contains any trivial mistakes, or if I missed an easier solution. I would like to achieve something like this:
class ClassA {
typedef double (ClassA::*CondFunc)();
public:
ClassA(int x, int y) {
value_ = x;
switch (y) {
case 0:
condFunc_ = &ClassA::condA;
break;
case 1:
condFunc_ = &ClassA::condB;
default:
break;
}
}
~ClassA();
int value_;
CondFunc condFunc_;
double condA() { return 2.0*value_; }
double condB() { return 4.0*value_; }
void Test() {
int a = condFunc_(); // compile error
}
};
but get a compile error in Test(). Please note that this is a vastly simplified function and is not supposed to make any sense. I've searched this forum and elsewhere for answers, but am still not sure whether defining/calling such non-static member function pointers is even possible. The only plausible hint/solution I've come across employs a static wrapper function to achieve something similar. I'd be grateful for any help/clarifications.
You have to call the member pointer function like this:
int a = (this->*condFunc_)();