C++ Syntax to call a member function using it's function pointer - c++

The example below uses a function pointer to a member function of the class Blah. The syntax of the function pointer is clear to me. However when calling I had to put brackets around this->*funcPtr and I am not sure why this is required. I guess it is related to the way how C++ evaluates the expression. The compiler used is VS 2008.
#include <iostream>
using namespace std;
struct Blah {
void myMethod(int i, int k) {
cout << "Hi from myMethod. Arguments: " << i << " " << k << endl;
}
typedef void (Blah::*blahFuncPtr)(int, int);
void travelSomething(blahFuncPtr funcPtr) {
(this->*funcPtr)(1, 2);
// w/o the brackets I get C2064 in VS 2008
// this->*funcPtr(1, 2);
}
};
int main() {
Blah blah;
blah.travelSomething(&Blah::myMethod);
cin.get();
return 0;
}

The function call operator () takes higher precendence than the 'pointer to member' operator ->*.
See, for example, here.

Related

How do parentheses around the function name change which function is called?

I could not understand the second foo call in the code below. How does it call global foo function. Why does (foo) call struct A's int()? Can you help me?
#include <stdio.h>
#include <utility>
#include <iostream>
using namespace std;
namespace MySpace{
struct A{
operator int () const {
cout <<"operator" << endl;
return 1;
}
};
void foo(A){
std::cout<< "1" << endl;
}
}
void foo(int){
std::cout << "--2" << endl;
}
int main()
{
MySpace::A x;
foo(x);
(foo)(x);
return 0;
}
I could not understand the second foo call. How does it call global foo function. Why does (foo) call struct A's int()? Can you help me?
The 1st one works because ADL finds MySpace::foo and it wins in overload resolution against ::foo and gets called.
For the 2nd one, adding parentheses like (foo) prevents ADL; then MySpace::foo can't be found, only ::foo is found and gets called. A is converted to int implicitly (by A's conversion operator) for it to be called.
BTW: You can mark the conversion operator as explicit to forbid the implicit conversion from A to int. Then the 2nd one would fail. E.g.
namespace MySpace {
struct A{
explicit operator int () const {
cout <<"operator" << endl;
return 1;
}
};
void foo(A){
std::cout<< "1" << endl;
}
}

Can you make a vector of functions with parameters?

Is it possible to create a vector that has functions pushed back?
I've tried doing something with pointers, but it only works with functions without parameters.
For example,
#include <iostream>
#include <vector>
using namespace std;
void printInt();
int main()
{
vector<void (*)()> functionStack;
functionStack.push_back(printInt);
(*functionStack[0])();
}
void printInt()
{
cout << "function works!" << 123 << endl;
}
That works, but not what I need.
The correct version of that would be a function that has parameters: void printInt(int a) and you could call it with different values like 4 or -1 but from the vector functionStack.
It's probably more complex if the functions in the vector are with different parameters, so let's assume that every function has the same type and amount of parameters.
This:
void (*)()
is a function pointer taking no arguments. So change it to take the desired argument.
void (*)(int)
Like so:
void printInt(int x)
{
cout << "function works!" << x << endl;
}
int main()
{
vector<void (*)(int)> functionStack;
functionStack.push_back(printInt);
(*functionStack[0])(123);
}
You are correct in saying the functions must have the same type and number of parameters for this to be valid.
You basically had it already.
#include <iostream>
#include <vector>
using namespace std;
void printInt(int a);
int main()
{
// Just needed the parameter type
vector<void (*)(int)> functionStack;
// Note that I removed the () from after the function
// This is how we get the function pointer; the () attempts to
// invoke the function
functionStack.push_back(printInt);
(*functionStack[0])(42);
}
void printInt(int a)
{
cout << "function works! " << a << endl;
}
This is also a situation where std::function might be beneficial as well.
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
void printInt(int a);
int main()
{
// Similar syntax, std::function allows more flexibility at a
// lines of assembly generated cost. But it's an up-front cost
vector<std::function<void(int)>> functionStack;
functionStack.push_back(printInt);
// I don't have to de-reference a pointer anymore
functionStack[0](42);
}
void printInt(int a)
{
cout << "function works! " << a << endl;
}

Ellipsis as a constructor argument outside of any variadic templates

Consider the following code:
#include <iostream>
struct S
{
S(const char *p) { std::cout << '[' << p << ']'; }
};
int main()
{
S var(...); // <------
return 0;
}
It compiles fine on GCC 5.2 with -pedantic -pedantic-errors, but prints nothing. I have no idea what this syntax means and I'm unable to find any information about it.
Looks like it just prevents an object from being constructed, but I've never heard about such feature.
The question is: What ellipsis means when used as a constructor argument?
It should be a function prototype with variable-length arguments.
To make sure of it, I add two lines and got undefined reference error.
#include <iostream>
struct S
{
S(const char *p) {std::cout << '[' << p << ']';}
};
int main()
{
S var(...); // <------ function prototype declaration
var(); // attempt to call the declared function, which is not defined
var(1); // the same as above
return 0;
}

Declaration of the function pointer

What do I declare with the following definition:
void (*bar)(A*){ }; //1
My first thought was that I declare and define function pointer and a function the pointer point to. But it's wrong, because any call to the bar() leads to a segmentation fault:
#include <iostream>
#include <vector>
#include <memory>
struct A{ };
void foo(A*){ std:cout << "foo" << std::endl; }
void (*bar)(){ };
int main(){
bar();
}
Moreover, I can't imbed any statement into the "definition":
void (*bar)(A*){ std::cout << "foo" << std::endl };
yeilds compile-time error.
So, what does the declaration //1 mean?
This statement:
void (*bar)(A*){ };
declares a variable named bar of type void(*)(A*), ie "pointer to function taking pointer to A and returning void", and zero-initializes it. Thus, it's equivalent to this:
void (*bar)(A*) = nullptr;
Obviously, when calling this bar, a segfault should be no surprise.
It's not possible to declare a function and a pointer to that function in a single declaration.
When you say
void (*bar)(A*){ }; //1
it means "bar" is a function pointer which can point to some function which takes "A*" as parameter.
In your case, it is not pointing to any function yet.
to make it working use,
void (*bar)(A*) = foo;
This means you have declared a function pointer that points to nothing at the moment. You should able to validate that using a debugger.
void (*bar)(A*){ }; //1
You could make the pointer point to a function like this:
void foo(A*){ std::cout << "foo" << std::endl };
bar = &foo;
And call it like this now:
A a;
bar(&a);
Full snippet:
#include <iostream>
class A {};
void (*bar)(A*){};
void foo(A*) { std::cout << " foo " << std::endl;}
int main() {
A a;
bar = &foo;
bar(&a);
}
Your code should be changed to the following code.
#include <iostream>
#include <vector>
#include <memory>
struct A{ };
void foo(A*){ std::cout << "foo" << std::endl; }
void (*bar)(A*);
int main(){
A a;
bar = &foo;
bar(&a);
}
To declare an actual function, get rid of the (*) portion around the function name:
void bar(A*){ std::cout << "foo" << std::endl };
https://ideone.com/UPIYxg
So, what does the declaration //1 mean?
It is just a comment.

C++ Function Pointer as Argument

I have tried multiple google searches and help guides, but I'm out of ideas on this one. I have a function pointer that I am using as an argument for another function. Both functions are within the same class. However, I keep getting type conversion errors. I'm sure this is just a syntax problem, but I can't understand what the correct syntax is. Here is a simplified version of my code:
Header File
#ifndef T_H
#define T_H
#include <iostream>
#include <complex>
namespace test
{
class T
{
public:
T();
double Sum(std::complex<double> (*arg1)(void), int from, int to);
int i;
std::complex<double> func();
void run();
};
}
#endif // T_H
Source File
#include "t.h"
using namespace test;
using namespace std;
//-----------------------------------------------------------------------
T::T()
{
}
//-----------------------------------------------------------------------
double T::Sum(complex<double>(*arg1)(void), int from, int to)
{
complex<double> out(0,0);
for (i = from; i <= to; i++)
{
out += arg1();
cout << "i = " << i << ", out = " << out.real() << endl;
}
return out.real();
}
//-----------------------------------------------------------------------
std::complex<double> T::func(){
complex<double> out(i,0);
return out;
}
//-----------------------------------------------------------------------
void T::run()
{
Sum(&test::T::func, 0, 10);
}
Whenever I try to compile, I get the following error:
no matching function for call to 'test::T::Sum(std::complex<double> (test::T::*)(),int,int)'
note: no known conversion for argument 1 from 'std::complex<double> (test::T::*)()' to 'std::complex<double>(*)()'
Any advice appreciated. Or at least a link to a thorough site on how to use function pointers. I am using Qt Creator 2.6.2, compiling with GCC.
Your Sum function expects pointer to a function. And then you try to call it with a pointer to a member function. Learn about pointers to members.
The code itself is a bit messy, I'll only correct the grammer to make it work.
firstly, you shall change the function prototype from
double Sum(std::complex<double> (*arg1)(void), int from, int to);
to
double Sum(std::complex<double> (T::*arg1)(void), int from, int to);
Meaning that it is a pointer to class T's member.
Then, when calling the function, you cant just arg1(),
for (i = from; i <= to; i++)
{
out += arg1();
cout << "i = " << i << ", out = " << out.real() << endl;
}
you have to use (this->*arg1)();
for (i = from; i <= to; i++)
{
out += (this->*arg1)();
cout << "i = " << i << ", out = " << out.real() << endl;
}
How to pass functions as arguments in C++? In general, use a template, unless you have very compelling reasons not do it.
template<typename Func>
void f(Func func) {
func(); // call
}
On the call side, you can now throw in a certain amount of objects (not just pointers to functions):
Functors;
struct MyFunc {
void operator()() const {
// do stuff
}
};
// use:
f(MyFunc());
Plain functions:
void foo() {}
// use
f(&foo) {}
Member functions:
struct X {
void foo() {}
};
// call foo on x
#include <functional>
X x;
func(std::bind(&X::foo, x));
Lambdas:
func([](){});
If you really want a compiled function and not a template, use std::function:
void ff(std::function<void(void)> func) {
func();
}