Pass reference to external function in class constructor - c++

There are other questions regarding how to pass an external function as an argument to a function in C++, but I am struggling to apply those answers to the case where the target function is the constructor function for a novel class.
Here is what I am trying to do:
#include <iostream>
double external_function(double x, double y) {
return x * y;
}
class foo {
public:
// Initialize a function (?) as a method of class foo
double func(double, double);
// Construct an instance of the class by supplying a pointer to an external function
foo(double(*func_in)(double, double)) {
func = *func_in;
}
// When calling this method, evaluate the external function
double eval(double x, double y) {
return func(x, y);
}
};
int main() {
foo foo_instance(&external_function);
std::cout << foo_instance.eval(1.5, 2); // Should print 3
return 0;
}
I would like to have func as a method of the foo class because I later will write methods of foo that do other things with func, e.g. search for a local minimum.
By analogy, here is working code that passes an external constant instead of a function:
#include <iostream>
double external_value = 1.234;
class bar {
public:
// Initialize a value as a method of class bar
double val;
// Construct an instance of the class by supplying a pointer to an external value
bar(double* val_in) {
val = *val_in;
}
// When calling this method, return the external function
double eval() {
return val;
}
};
int main() {
bar bar_instance(&external_value);
std::cout << bar_instance.eval(); // 1.234
return 0;
}

Like this
class foo {
public:
// Initialize a function pointer as a method of class foo
double (*func)(double, double);
// Construct an instance of the class by supplying a pointer to an external function
foo(double(*func_in)(double, double)) {
func = func_in;
}
// When calling this method, evaluate the external function
double eval(double x, double y) {
return func(x, y);
}
};
In your version func was an undefined method of the class not the function pointer you wanted it to be.

Here is the example using std::function:
#include <functional>
class foo {
public:
// Initialize a function as a method of class foo
std::function<double(double, double)> func;
// Construct an instance of the class by supplying a pointer to an external function
foo(std::function<double(double, double)> func_in) {
func = func_in;
}
// When calling this method, evaluate the external function
double eval(double x, double y) {
return func(x, y);
}
};

Related

parameter list type error in lambda function

I'm not very familiar with lambda expressions C++11. I was trying to simply invoke a method from another class taking an integer and constructing a lambda expression but I'm receiving an error about the parameter not being the correct datatype.
class A{
int _a;
void f(int a){
_a = a;
}
};
class B{
B(){
A instance = new A();
instance.f(
[&](int input)->int
{
int x = 2;
return x;
});
};
}
A lambda is really just a compact way of writing a function.
The lambda in question:
[&](int input) -> int
{
int x = 2;
return x;
};
is a unnamed function, taking one int parameter (that it does not use)
and returning int. It also capture its context with reference semantics,
something it also does not make use of.
If you want to use a lambda in conjunction with a function that expects
an int, you need to call the lambda, maybe like this:
class A
{
public:
void f(int a){
_a = a;
}
private:
int _a;
};
class B
{
public:
B()
{
A instance; // = new A(); <- not C++
auto mylambda =
[](int input) -> int
{
int x = 2;
return x;
};
instance.f( mylambda(3) );
}
};

Template pass member function with args and return value as parameter c++

I would like to pass a templated function args for class and a method of that class to call. The method has arguments as well as a return value.
Here is what I have so far. I believe I'm getting a little tripped up on the templatized function syntax:
bar.h
class Bar {
public:
Bar();
int FuncBar(int arg1);
}
foo.h
template<typename A, int (A::*Method)()>
int FuncTemplate(A* a, int bar_arg1) {
....
return a->Method(bar_arg1)
}
class Foo {
public:
explicit Foo(Bar* bar);
private:
void FuncFoo();
Bar* bar_;
}
foo.cc
Foo::Foo(Bar bar) : bar_(bar) {};
void Foo::FuncFoo() {
...
int bar_arg1 = 0;
FuncTemplate<Bar, &(*bar_)::FuncBar>(bar_, bar_arg1);
}
You need to use int (A::*Method)(int) as the function pointer type since the member function expects an int as an argument.
Also, the call to the member function needs to be (a->*Method)(bar_arg1). The syntax for calling member function using a member function is not very intuitive.
template<typename A, int (A::*Method)(int)>
int FuncTemplate(A* a, int bar_arg1) {
....
return (a->*Method)(bar_arg1)
}
Also, you need to use &Bar::FuncBar to get a pointer to the member function, not &(*bar_)::FuncBar.
void Foo::FuncFoo() {
int bar_arg1 = 0;
FuncTemplate<Bar, &Bar::FuncBar>(bar_, bar_arg1);
// ^^^^^^^^^^^^^
}

Using a custom constructor as a template function

I have template function change that takes a function that takes int and returns an object of type A. So I thought I can use the constructor of A
class A {
int y;
public:
explicit A(int y) : y(2 * y) {
}
};
class B {
A x;
public:
B(int x) : x(x) {
}
template<typename F>
void change(int y, F func) {
x = func(y);
}
};
int main(void) {
B b(7);
b.change(88, A()); // << here
return 0;
}
But the compiler says no matching function for call to ‘A::A()’
How can I make it works?
You can't pass a constructor as a parameter like you are attempting. The C++ standard is very strict on not allowing the memory address of a constructor to be taken.
When you call change(88, A()), you are actually constructing a temp A object (which the compiler should not allow since A does not have a default constructor) and then you are passing that object to the parameter of change(). The compiler is correct to complain, since A does not define an operator()(int) to satisfy the call to func(y) when called in an A object.
To make this work, you need to create a separate function that constructs the A object, and then pass that function to change(), eg:
A createA(int y)
{
return A(y);
}
int main(void) {
B b(7);
b.change(88, createA);
return 0;
}

passing a pointer to a class method as a function argument [duplicate]

This question already has answers here:
How do you pass a member function pointer?
(6 answers)
Closed 9 years ago.
I have a class
class A{
A(/*constructor arguments*/);
double MethA(double);
};
And I want to pass the method MethA in a function that takes a pointer to a function :
double function(double (*f)(double), double x){
return f(x);
}
So what I'm doing is to call
A a(/*constructor arguments*/);
function(a.MethA,1.0);
but it doesn't compile.
I'm pretty sure that this question is answered somewhere else, but I couldn't find where because I'm not sure that the terminology I use is correct. Am I trying to pass a pointer on a class method as a function argument ? Or, to pass a function pointer as a member of a class... I'm confused :-(
When you need to use a pointer to member function, you need to pass two separate things:
what member function to call and
what instance to call it on.
In C++, you can't combine them in one construct, like you want to:
A a;
bar(a.foo);
is not valid C++.
Instead, you have to do this:
A a;
bar(a, &A::foo)
And declare and implement bar() accordingly:
void bar(A &a, void (A::*method)()) {
a.*method();
}
See Arkadiy's answer if you want to see how to properly use member function pointers.
BUT
As requested in the comments: if the compiler you are using supports lambdas (some without full C++11 do). You can do something like the following, which looks more like the syntax you are attempting to use.
Your definition for function changes to something like:
template <typename F>
double function(F f, double x){
return f(x);
};
a function template that accepts a parameter that is callable with a double.
At your call-site you do this:
A a(/*constructor arguments*/);
function([&](double x){return a.MethA(x);},1.0);
That generates a function object in-place that is bound to your class instance a by reference.
The template can be made fully typesafe with some magic in <type_traits>, but as-is it will give you template spew if you pass something very wrong.
It has to be a static function!
#include <iostream>
#include <cassert>
class A {
public:
static double MethA(double x) { return 5 * x; }
};
typedef double (*ftype)(double);
double function(ftype f) {
assert(f != NULL);
return f(7);
}
int main(int, char**) {
// expect "35\n" on stdout
std::cout << function(A::MethA) << "\n";
}
It has to be static because you can't access any of A's variables without knowing which A object are you refering to! If you need A's non-static member variables, you need to pass a reference to an a into the static function:
#include <iostream>
#include <cassert>
class A {
double fX;
public:
A(double x) : fX(x) { }
double methB(double x) const { return fX * x; }
static double MethB(double x, const A& a) {
return a.methB(x);
}
};
typedef double (*ftype2)(double, const A&);
double function_with_context(ftype2 f, const A& a) {
assert(f != NULL);
return f(7, a);
}
int main(int, char**) {
A a(6);
// expect "42\n" on stdout
std::cout << function_with_context(A::MethB, a) << "\n";
}
But it's sometimes better to use inheritance and polymorphism to achieve this sort of interface:
#include <iostream>
class MyInterface {
public:
virtual double f(double x) const = 0;
};
class A : public MyInterface {
double fX;
public:
A(double x) : fX(x) { }
double f(double x) const {
return fX * x;
}
};
double function(const MyInterface& o) {
return o.f(7);
}
int main(int, char**) {
A a(6);
// expect "42\n" on stdout
std::cout << function(a) << "\n";
}

How to create a new function from a method, with fewer arguments?

I have two classes, ClassA and ClassB.
ClassA has three methods:
double Foo(double, ClassB);
double Bar(double (*f)(double));
double Baz(double, ClassB);
I would like to define a function Qux inside Foo, based on Baz but without the argument of type ClassB: i.e. of the kind "double Qux(double)" so that I can pass it to Bar:
double ClassA::Foo(double x, ClassB y)
{
// double Qux(double .) = Baz(., y)
Bar((*Qux))
}
Does some one have any idea?
I guess some will answer this is not the good way to do it. So just to explain the concrete situation, I am pricing financial assets using a numerical method (http://en.wikipedia.org/wiki/Simpson%27s_rule) in order to compute integrals:
ClassA: FinancialAsset
ClassB: PrincingModel
Foo: FinancialAsset.Price(date, PrincingModel)
Bar: FinancialAsset.SimpsonMethod(FunctionOneArgument)
Baz: FinancialAsset.FunctionTwoArguments(date, PrincingModel)
And I am looking for:
Qux: FunctionOneArgument(date) = FinancialAsset.FunctionTwoArguments(date, PrincingModel)
I am not sure what is the good way to address this structure. I you have a better / more c++'s way to do it, I'll take :)
Thanks
You can't do that exactly, because your Bar function is taking a pointer to a regular function, but you can use this instead:
class A {
...
public:
double Foo(double, ClassB);
double Bar(std::function<double(double)> f);
double Baz(double, ClassB);
};
double ClassA::Foo(double x, ClassB y)
{
auto Qux = [&](double x) { Baz(x,y); };
return Bar(Qux);
}
std::function is a more general way of representing function-like objects. You can convert a regular function, a lambda, or a function object to it.
Depending on whether you have C++11 or not, you either want std::bind and std::function or boost::bind and boost::function for older C++ versions.
binding allows you to take a function and bind 0 or more of the parameters, or rearrange the parameters. Indeed something you have above would look like this:
double ClassA::Foo(double x, ClassB y)
{
boost::function<double> baz = boost::bind(this, ClassA::Baz, _1, y);
Bar(baz);
}
And Bar's signature would take a boost::function instead of a function pointer.
Note my syntax might be slightly off for binding memeber functions, have a look at the documentation for details.
see here:
http://www.boost.org/doc/libs/1_53_0/libs/bind/bind.html
Or here: http://en.cppreference.com/w/cpp/utility/functional/bind
You can do it without changing any function signatures (and C++11 or boost), but I wouldn't suggest it if you can avoid it. It's ugly, not thread-safe, and in general not very nice:
#include <iostream>
struct B
{
// some data in B
int i;
};
struct A
{
//some data in A
double d;
// the functions you defined in A
double Foo(double x, B y);
double Bar(double (*f)(double));
double Baz(double x, B y);
};
// a poor substitutes for closures
struct
{
A *a;
B *b;
} hack;
double Qux(double x2)
{
// use the stored pointer to call Baz on x2
return hack.a->Baz(x2, *hack.b);
}
double A::Foo(double x, B y)
{
// store pointers for use in Qux
hack.a = this;
hack.b = &y;
// do something with x
d += x;
double result = Bar(&Qux);
return result;
}
double A::Bar(double (*f)(double))
{
// do something with d, call the passed function
d += 1;
return f(d);
}
double A::Baz(double x, B y)
{
// do something with the passed data
return x + y.i;
}
int main()
{
A a;
a.d = 1.25;
B b;
b.i = 2;
std::cout << a.Foo(.25, b) << std::endl; // should be 4.5
return 0;
}