callback function in c++ - c++

The code describes two classes that implement callback function, the function must should be member function in the class parameter that passed in template.
Below the code i attached the relevent error message i get.
a.h
template <class CLASSNAME>
class a
{
public:
typedef void (CLASSNAME::*myFunction)();
a(CLASSNAME& myObject, myFunction callback) :
m_myObject(myObject)
{
m_myFuntion = callback;
}
void update()
{
(m_myObject).*(m_myFuntion);
}
myFunction m_myFuntion;
CLASSNAME& m_myObject;
};
dummy.h
#include <stdio.h>
class dummy
{
public:
dummy()
{
var = 14;
}
void func()
{
printf("func!!");
}
int var;
};
main.cpp
#include <cstdlib>
#include "a.h"
#include "dummy.h"
void main()
{
dummy dum;
a<dummy> avar(dum, &(dummy::func));
avar.update();
system("pause");
}
i am trying to implement the callback function and i get the following error message:
C2298 missing call to bound pointer to member function
what the problem is?

You have a lot of parentheses, they're just not in the right place. The correct syntax for calling a pointer-to-member function is:
void update()
{
(m_myObject.*m_myFuntion)();
}

You are using parentheses in the wrong places:
This:
a<dummy> avar(dum, &(dummy::func));
should be this:
a<dummy> avar(dum, &dummy::func);
And this:
(m_myObject).*(m_myFuntion);
should be:
(m_myObject.*m_myFuntion)();
Live Example

Related

How to execute a function out of an included class

I got two classes separated in four files. The main class includes a sub class and needs to execute functions of it (not shown in the minimal example code). What I want to do is to execute a function of the main class in the scope of the subclass.
I think some ideas would be to inherit the functions in the sub class but I could not figure out how to do this.
MainClass.cpp
#include "MainClass.hpp"
void MainClass::mainCallback() {
std::cout << "[mainCallback] executed" << std::endl;
}
void MainClass::subCallback() {
std::cout << "[subCallback] executed" << std::endl;
}
int main() {
MainClass mainClass;
mainClass.mainCallback();
SubClass subClass;
subClass.activateSubClass();
return 0;
}
MainClass.hpp
#pragma once
#include "SubClass.hpp"
#include <iostream>
class MainClass{
public:
void mainCallback();
void subCallback();
};
SubClass.cpp
#include "SubClass.hpp"
void SubClass::activateSubClass(){
mainClass.subCallback(); //TODO call this function from this scope
}
SubClass.hpp
#pragma once
class SubClass{
public:
void activateSubClass();
};
The error in SubClass.cpp is of course:
error: use of undeclared identifier 'mainClass'
Just subclass the subclass:
class SubClass: public MainClass {
public:
void activateSubClass();
};
This (public) way the SubClass makes all methods of MainClass callable in SubClass instances. You could also private inherit. That way only activateSubClass() 'ld be callable.
In activateSubClass you can call directly the methods of the parent class:
void SubClass::activateSubClass(){
mainClass.subCallback(); //TODO call this function from this scope
}
Don't forget to include MainClass.hpp in SubClass.hpp
You try to call a MainClass.subCallback() without having an instance of MainClass. According to me, this is the typical use case for static methods.
Then, I think you make your #include directives the wrong way. Indeed, MainClass does not seem to need to know SubClass but the opposite is true. I think it is better to include MainClass.hpp in SubClass.hpp. This will solve your circle dependencies problem.And you can write your main() function in another file.
EDIT: Example
MainClass.hpp:
#pragma once
class MainClass
{
public:
void mainCallback();
static void subCallback(); // mak it static to be able to call it without an instance of the class
};
MainClass.cpp:
#include "MainClass.hpp"
#include <iostream>
void MainClass::mainCallback()
{
std::cout << "[mainCallback] executed" << std::endl;
}
void MainClass::subCallback()
{
std::cout << "[subCallback] executed" << std::endl;
}
SubClass.hpp:
#pragma once
class SubClass
{
public:
void activateSubClass();
};
SubClass.cpp:
#include "SubClass.hpp"
#include "MainClass.hpp" // MainClass inclusion
void Suclass::activateSubClass()
{
MainClass::subCallback(); // Call of the static method
}
main.cpp:
#include "MainClass.hpp"
#include "SubClass.hpp"
int main()
{
MainClass mc;
mc.mainCallback();
SubClass sc;
sc.activateSubClass(); // Will call MainClass::subCallback()
return 0;
}
I hope it can help you.

Set reference of function to non-static function from other c++ file

I'm trying to set a reference of a non-static function in c++. The function I'm referencing is not from the same c++ file, and I get and error saying :
Cannot create a non-constant pointer to member function.
Main.cpp
#include <iostream>
#include "Test.hpp"
class testClass {
public:
void (*update) (void);
};
int main() {
testClass tc;
test t;
tc.update = &t.update; //This is where the error occurs
return 0;
}
Test.hpp
#ifndef Test_hpp
#define Test_hpp
#include <stdio.h>
class test {
public:
void update() {
//Do something
}
};
#endif /* Test_hpp */
My question is how do you do this without setting update in test class to static?
static void update() {
//Do something
}
Using this code it works, but like I've stated I do not want this functiont to be static.
EDIT :
Because I'm stupid I failed to mention that the class test should be able to be different. Also to the answers I got already I learned that tc.update = &t.update; is wrong.
For Example :
#include <iostream>
#include "Test.hpp"
#include "anotherTestClass.hpp"
//I do not want to use templates if possible
class testClass {
public:
void (*update)(void);
};
int main() {
testClass tc;
test t;
tc.update = &test.update; //I know this is wrong now.
testClass tc2;
anotherTestClass atc;
tc2.update = &atc.update;
//p.s. I'm bad with c++
}
And the error i get now is.
Assigned to 'void (*)()' from incompatible type 'void (test::*)()'
One more thing is I'm using XCode to program, which I believe uses LLVM-GCC 4.2 as the compiler.
class test {
public:
void update() {
//Do something
}
};
class testClass {
public:
void (test::* update) (void);
};
int main() {
testClass tc;
test t;
tc.update = &test::update;
return 0;
}
Your approach is essentially wrong.
Member Function Pointers.
The member in the testClass:
void (*update) (void);
is a function pointer, which is different to a method function pointer. That why in order to compile you should switch to a static method (which is essentially a "normal" function).
A method function pointer should containt the static information about the class the method belongs.
Practically the right way is:
void (test::* ptr_method)(void); // a member pointer to test class
In that way the variable named ptr_method is a method of the class test pointer.
Then,
Get the Address of a method.
Your statement:
tc.update = &t.update; //This is where the error occurs
is simply wrong. The address of a class method is something which is not related with the object of that class.
You can obtain the address of a method with the syntax:
&CLASS_NAME::METHOD_NAME;
Indeed, that statement should be something like:
tc.update = &test::update;
Additional suggestions.
Call a method by means of a method pointer.
Once you have a method pointer it is not so immediate to call the method associated with it.
As I said before, the address of the method is not related with the object of that class, so if you want to call the method you need to provide to the compiler the information about the object on which the method has to be called.
The syntax is something like:
(OBJECT.*METHOD_POINTER)(ARGS...);
Here, I propose a simple demo which shows all what I've just said.

c++ store functor in class

I´ve created the following Event class:
Event.h
#ifndef EVENT_H
#define EVENT_H
#include<string>
template<typename T>
class Event
{
public:
T fnktptr; //Error: field ‘Event<int()>::fnktptr’ invalidly declared function type
Event();
virtual ~Event();
};
#endif // EVENT_H
Event.cpp
#include "Event.h"
template<typename T>
Event<T>::Event()
{
//ctor
}
template<typename T>
Event<T>::~Event()
{
//dtor
}
// No need to call this TemporaryFunction() function,
// it's just to avoid link error.
void TemporaryFunction ()
{
Event<int> TempObj;
}
Now I tested it with the following code and it works:
Event<int> event;
int t = 5;
event.fnktptr = t;
But finally, I want to use it with a functor like this:
Event<decltype(consumeInt)> event;
event.fnktptr = consumeInt;
But now I get a Error in the Event.h file:
T fnktptr; //Error: field ‘Event<int()>::fnktptr’ invalidly declared function type
Based on your latest comments, I would say a std::function<> is the best way forward. The example code below assumes you just want to call fnktptr and don't care about the result.
#include <functional>
class Event
{
public:
std::function<void ()> fnktptr;
Event();
virtual ~Event();
};
If you need to capture the result, you will not be able to arbitrarily have different returns for each Event object. You either must pick one or use something like boost::variant<>.
template<class T>
class Event
{
public:
std::function<T ()> fnktptr;
Event();
virtual ~Event();
};
This would allow you to create Event<int> objects for instance to use with consumeInt().
Example:
Event<int> event;
event.fnktptr = consumeInt;

C++ Pass a member function to another class

I'm trying to make a simple callback in C++ but I'm having issues doing it as I want to.
Essentially I want something like this:
class A{
void A::AddCallback(void (*callBackFunction)(string)){
/*Code goes here*/
}
}
And class B
#include "A.h"
class B{
B::B(){
A childObject;
childObject(MyCallBackFunction);
}
B::MyCallBackFunction(string arg){
/*Code goes here*/
}
}
I know that usually you would want to define the header of AddCallback with something like B::callBackFunction but I do need to import A in B so I it would be awkward to have both classes import each other. I know I've seen this before but I cant get the syntax right
Here is one option using a static member function:
#include <string>
struct A
{
void AddCallback(void (*cb)(std::string));
};
struct B
{
A a;
B() { a.AddCallback(&B::MyFun); }
static void MyFun(std::string);
};
If you want a non-static member function, then you first need to decide on which instance of B you want the member function to be invoked. For example, to invoke it on the object whose constructor registers the callback:
#include <functional>
#include <string>
struct A
{
void AddCallback(std::function<void(std::string)>);
};
struct B
{
A a;
B() { a.AddCallback(std::bind(&B::MyFun, this, std::placeholders::_1)); }
void MyFun(std::string);
};
you must call void A::AddCallback and pass callback instead of passing argument in childObject(MyCallBackFunction);

C++ Class instantiating and calling method within

Alright, I just thought give C++ a try (I currently do C#), now my first problem is class instantiating or calling a method in another class.
Here is my code:
My main Program entry:
#include <iostream>
#include "myClass.h"
using namespace std;
int main()
{
myClass* mc = new myClass();
}
The Class I'm trying to access:
#include "myClass.h"
myClass::myClass()
{
void DoSomething();
{
}
}
myClass::~myClass()
{
}
The Class Header:
#pragma once
class myClass
{
public:
myClass();
~myClass();
};
Now as you can see i have instantiated the class, but i cant access DoSomething() method.
This code simply declares a local function, and has an empty scope for fun:
myClass::myClass()
{
void DoSomething(); // local function declaration
{
// empty scope
}
}
If you want doSomething() to be a member of myclass, you have to declare it in the class definition:
class myClass
{
public:
myClass();
~myClass();
void doSomething();
};
then implement it
void myclass::doSomething() { .... }
Also note that you don't have to use new everywhere in C++. You can instantiate an object like this:
int main()
{
myClass mc;
}
Also note that using namespace std; isn't such a great idea anyway, at least not in real code.
this is a function declaration in a constructor (+an ampty scope within { }).
myClass::myClass()
{
void DoSomething();
{
}
}
You want a function declaration in class body (not in constructor) and definition of this function (this can be done immediately together with declaration or later as i.e. here):
class myClass
{
public:
myClass();
~myClass();
void doSomething();
};
implementation of constructor:
myClass::myClass() { .... }
implementation of your function, similarly:
void myClass::doSomething() { .... }
The method you are trying to use in main must be part of the class definition. You can write an inline function or can have a separate definition of that function. Also, you need to make that function public in order to access it from main.