C++ using passed function - c++

I have two functions one of them takes a function as an argument this works just fine, but I want to call this passed function in my second one.
class XY {
public:
void first(void f());
void second();
};
void XY::first(void f()){
}
void XY::second(){
f(); //passed function from first()
}

You might use std::function to store the callable and call it later.
class X {
public:
void set(std::function<void()> f) {
callable = f;
}
void call() const {
callable();
}
private:
std::function<void()> callable;
};
void f() {
std::cout << "Meow" << std::endl;
}
Then create X instance and set the callable:
X x;
x.set(f);
Later call the stored callable:
x.call();

Related

How to pass function of one class as argument of another function of another class

I have created a class Base which has a function addSuccessor() that takes address of function as argument and stores it in successor. How do I pass a function of another object in addSuccessor().
Here is my program. I think my main() has some mistake.
#include <iostream>
#include<vector>
using namespace std;
class Base{
public:
void (*successor)()=NULL;
void addSuccessor ( void (*f)() )
{
successor=f;
}
void start()
{
cout<<"In Class"<<endl;
if(!successor==NULL)
successor();
else
cout<<"No Successor"<<endl;
}
};
class Second{
public:
void foo()
{
cout<<"Successor";
}
};
int main()
{
Base obj;
Second obj2;
obj.addSuccessor(&obj2.foo());
obj.start();
}
Function pointers are very limited. Use a std::function (defined in header <functional>) instead, which can store any invocable object in a type-erased manner, and provide a template member function to set the functor in which perfect forwarding is used to forward the provided functor to successor: (std::forward is defined in header <utility>)
class Base {
std::function<void()> successor;
public:
template <typename F>
void add_successor(F&& f)
{
successor = std::forward<F>(f);
}
void start()
{
if (successor) {
successor();
} else {
std::cout << "No Successor\n";
}
}
};
Then, you can pass a lambda expression to add_successor:
obj.add_successor([&]{ obj2.foo(); });
(live demo)
As a complement to the discussion above (based on #L.F.'s answer):
#include <functional>
#include <iostream>
#include <utility>
class Base {
std::function<int(int)> successor;
public:
template <typename F>
void add_successor(F&& f)
{
successor = std::forward<F>(f);
}
void start()
{
if (successor) {
auto z = std::invoke(successor, 100);
std::cout << "Output = " << z << "\n";
} else {
std::cout << "No Successor\n";
}
}
};
class Second {
public:
int foo(int x)
{
auto y = x + x;
std::cout << "Successor\n";
return y;
}
};
int main()
{
Base obj;
Second obj2;
obj.add_successor([&](int x)->int { return obj2.foo(x); });
obj.start();
}

Difference between func() and (*this).func() in C++

I am working on someone else code in C++, and I found a weird call to a certain function func(). Here is an example:
if(condition)
func();
else
(*this).func();
What is the difference between func() and (*this).func()?
What are the cases where the call to func() and (*this).func() will execute different code?
In my case, func() is not a macro. It is a virtual function in the base class, with an implementation in both base and derived class, and no free func(). The if is located in a method in the base class.
There actually is a difference, but in a very non-trivial context. Consider this code:
void func ( )
{
std::cout << "Free function" << std::endl;
}
template <typename Derived>
struct test : Derived
{
void f ( )
{
func(); // 1
this->func(); // 2
}
};
struct derived
{
void func ( )
{
std::cout << "Method" << std::endl;
}
};
test<derived> t;
Now, if we call t.f(), the first line of test::f will invoke the free function func, while the second line will call derived::func.
It is impossible to tell from the snippet but possibly there are two callable objects called func(). The (*this).func(); makes sure the member function is called.
A callable object could be (for example) a functor or a lambda expression:
functor
struct func_type
{
void operator()() const { /* do stuff */ }
};
func_type func; // called using func();
lambda
auto func = [](){ /* do stuff */ }; // called using func();
For example:
#include <iostream>
class A
{
public:
// member
void func() { std::cout << "member function" << '\n'; }
void other()
{
// lambda
auto func = [](){ std::cout << "lambda function" << '\n'; };
func(); // calls lambda
(*this).func(); // calls member
}
};
int main()
{
A a;
a.other();
}
Output:
lambda function
member function
Another case when those two lines will call different functions:
#include <iostream>
namespace B
{ void foo() { std::cout << "namespace\n"; } }
struct A {
void foo() { std::cout << "member\n"; }
void bar()
{
using B::foo;
foo();
(*this).foo();
}
};
int main ()
{
A a;
a.bar();
}
With type dependent name, it may be different:
void func() { std::cout << "::func()\n"; }
struct S {
void func() const { std::cout << "S::func()\n"; }
};
template <typename T>
struct C : T
{
void foo() const {
func(); // Call ::func
(*this).func(); // Call S::func
}
};
Demo

assigning member function to be other function

Can a function be assigned or alter its definition, just like overriding a function by derived classes.
But at this time, it is out of scope from the class.
I mean like this:
//class.h
class MClass
{
public:
void function(); // this is the function I am referring to.
}
//class.cpp
void MClass::function() { }
//file1.cpp
MClass mclass;
void globalFunction() { }
mclass.function = globalFunction; //is this even possible?
Your function() is a real function, you want function Callback instead.
see sample program: on ideone
#include <functional>
class MClass
{
public:
std::function<void(void)> func_ptr;
void setFuncPtr(const std::function<void(void)>& ptr)
{
func_ptr = ptr;
}
void callFuncPtr()
{
func_ptr();
}
};
You could set func_ptr to any callabe objet
MClass m;
m.setFuncPtr(globalFunction); // set to standalone function
m.callFuncPtr();
// set lambda to it
m.setFuncPtr([](){ std::cout << " do something " << std::endl; });
m.callFuncPtr();
Or bind to other object
struct Test
{
void print() { std::cout << "Test::print" << std::endl; }
};
Test t;
m.setFuncPtr(std::bind(&Test::print, t));
m.callFuncPtr();
You could also pass parameter to function by using std::bind and placeholders.
There are two ways :
c++03 - you can use function pointer :
class MClass
{
public:
void (*function)();
};
void globalFunction()
{
// ...
}
MClass obj;
mclass.function = globalFunction;
in c++11 you can use functors :
class MClass
{
public:
std::function<void()> function;
};
void globalFunction()
{
// ...
}
MClass obj;
mclass.function = globalFunction;

Passing Void into a class as a pointer then executing its contents

How can I pass a function as an argument and then execute it. I'm trying to do something like this:
class Foo{
private:
void (*external);
public:
Foo(void (*function)()){ *external = *function; }
~Foo(){ }
bool Execute(){
*external(); // Somehow execute 'external' which does the same thing with 'function'
return true
}
};
void pFnc(){
printf("test");
}
int main(){
Foo foo = Foo(&pFnc);
foo.Execute();
return 0;
}
This is not working of course.
You were close.
class Foo
{
public:
typedef void(*FN)(void);
Foo(FN fn) : fn_(fn) {};
bool Execute()
{
fn_();
return true;
}
FN fn_;
};
void pFunc(){
printf("test");
}
int main()
{
Foo foo(&pFunc);
foo.Execute();
}
Try:
void (*external)();
Your original declaration is a pointer to void, not a pointer to a function returning void.
Set it with
external = function;
and execute with
external();
Also, external has to be declared as a function pointer void (*external)(). Otherwise, you have to cast between function- and void-pointer.

functor call (additional characters)

I tried to build a minimal example:
struct Functor
{
void operator()(int& a)
{
a += 1;
}
void other(int& a)
{
a += 2;
}
};
template <typename foo>
class Class
{
public:
void function()
{
int a = 10;
foo()(a);
std::cout << a << std::endl;
}
};
int main()
{
Class<Functor> c;
c.function();
}
My question about this: Why is it even possible to call the operator on the pure type without an object? How can I call the function other the same way as I call operator()?
You're not calling it on a pure type. foo() invokes the constructor, and evaluates to a temporary foo object, on which you then invoke operator().
To do the equivalent with a "normal" member function, just do:
foo().other(a);
You are not "call[ing] the operator on the pure type without an object". The syntax foo()(a) is creating a temporary of type foo (this is the foo() part) and then calling operator() on that object with a as argument: (the (a) part).
Pure type example:
struct Functor
{
void operator()(int& a)
{
a += 1;
}
void other(int& a)
{
a += 2;
}
static void one_more(int& a)
{
a += 3;
}
};
template <typename foo>
class Class
{
public:
void function()
{
int a = 10;
foo()(a);
foo().other(a);
foo::one_more(a);
std::cout << a << std::endl;
}
};
int main()
{
Class<Functor> c;
c.function();
}