'c++ std::function callback
I have the below five files.
I am trying to call a function in two_b.cpp from one_a.cpp using a std::function callback, but I get the following error:
terminate called after throwing an instance of 'std::bad_function_call'
what(): bad_function_call
Aborted (core dumped)
one_a.h
#include <functional>
using CallbackType = std::function<void()>;
class Car {
public:
void car_run(void);
void car_stop(void);
CallbackType f1;
private:
CallbackType callbackHandler;
};
one_a.cpp
#include "one_a.h"
#include <iostream>
void Car::car_run(void)
{
std::cout<<"Car running"<<std::endl;
}
void Car::car_stop(void)
{
std::cout<<"Car stopping"<<std::endl;
}
two_b.h
#include "one_a.h"
class Boat {
public:
Boat(Car& car_itf);
static void boat_run(void);
static void boat_stop(void);
private:
Car& car_itf_;
};
two_b.cpp
#include "two_b.h"
#include <iostream>
Boat::Boat(Car& car_itf):car_itf_{car_itf}{
car_itf_.f1 = std::bind(&Boat::boat_run);
}
void Boat::boat_run(void)
{
std::cout<<"Boat running"<<std::endl;
}
void Boat::boat_stop(void)
{
std::cout<<"Boat running"<<std::endl;
}
main.cpp
#include "two_b.h"
int main()
{
Car bmw;
bmw.car_run();
bmw.f1();
return 0;
}
A running example can be found here:
Solution 1:[1]
std::function throws a std::bad_function_call exception if you try to invoke it without having a callable target assigned to it.
So, you need to actually assign a target to f1 before you try to invoke f1 as a function. You are doing that assignment in Boat's constructor, so you need to create a Boat object first, eg:
int main()
{
Car bmw;
Boat ferry(bmw); // <--
bmw.car_run();
bmw.f1();
return 0;
}
Also, make sure to clear f1 in Boat's destructor, so you don't leave the f1 dangling, eg:
Boat::~Boat(){
car_itf_.f1 = nullptr;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
