I struggle creating derived classes and passing method pointers from it to the base class, so that a function declared in the base class may call it (call the function of the derived class via interface).
The goal is to create derived classes to bring their own ressources and functions, but the call of a function declared there should be possible by calling one of them in the function the base class provides. For this i need to pass a member function pointer of the derived down to the base class.
Here's what I tried:
class KeyFunction
{
void(*KeyFunction::KeyFuncPtr)() = nullptr; //pointer to a method of this class to store standard behavior for call
public:
KeyFunction(void(*KeyFunction::KeyFunc)()) : KeyFuncPtr(KeyFunc) {} //constructor which takes in standard behavior
void func() //standard call of the function
{
if(KeyFuncPtr)KeyFuncPtr(); //call with ensurance there's something to be called
}
void operator()() //make KeyFunction class callable
{
func();
}
};
class customRessource{
public:
string up = "UP";
string down = "DOWN";
};
class customKeyFunc : public KeyFunction
{
customRessource& r;
public:
void moveup() //possible behavior
{
cout << r.up;
}
void movedown()
{
cout << r.down;
}
customKeyFunc( void(*customKeyFunc::KeyFunc)() ) :KeyFunction( ( void(*KeyFunction::)() ) (KeyFunc) ){}
};
int main()
{
customKeyFunc Up(&(customKeyFunc::moveup)); //setup functions
customKeyFunc Down(&customKeyFunc::movedown);
Up(); //call functions
Down();
getchar();
return 0;
}
The main function at the end shows the supposed way to use the class .
First of all: my types in the constructors of each class go wild (i tried a lot of search about how to write member pointers right but i'm still not stable with the syntax)
Can someone help me get them right ?
Can I even do this (especially casting down member pointers like i did in the customKeyFunc constructor)? Am I aproaching this the right way or do I think too complicated ?
Thank you in advance for your help !
something like this?
#include <functional>
#include <string>
#include <iostream>
class customResource{
public:
const std::string up = "UP";
const std::string down = "DOWN";
};
class customKeyFunc
{
const customResource& r;
public:
customKeyFunc(const customResource& r) : r(r) {}
void moveup() //possible behavior
{
std::cout << r.up;
}
void movedown()
{
std::cout << r.down;
}
};
int main()
{
customResource r;
customKeyFunc f(r);
auto Up = std::function<void()>(std::bind(&customKeyFunc::moveup, f));
auto Down = std::function<void()>(std::bind(&customKeyFunc::movedown, f));
Up(); //call functions
Down();
return 0;
}
std::function<void()> is a polymorphic functor which will copy any object that:
is movable or copyable, and
implements void operator()
Related
Following up my previous question about pointers to functions I'd like to ask, what is the difference between the pieces of code below.
This works! I use a pointer to a member function of the class Base. Though I have to call the function through the pointer differently cause it's a pointer to a member and not a pointer to a free function. But it works.
#include <iostream>
class Base;
using Handler = void (Base::*)(int e);
class Base
{
protected:
Base(Handler init)
{
currentState = init;
}
Handler currentState;
};
class Derived : public Base
{
public:
Derived() : Base((Handler)&foo)
{
}
void run(int e)
{
(this->*currentState)(e);
}
private:
void foo(int e)
{
std::cout << "foo function\n";
std::cout << "e = " << e << std::endl;
}
};
int main(void)
{
Derived derived;
derived.run(10);
return 0;
}
Output:
foo function
e = 10
Then I modify the code a little bit and I use pointer to function using Handler = void (*)(int e);
#include <iostream>
class Base;
using Handler = void (*)(int e);
class Base
{
protected:
Base(Handler init)
{
currentState = init;
}
Handler currentState;
};
class Derived : public Base
{
public:
Derived() : Base((Handler)&foo)
{
}
void run(int e)
{
currentState(e);
}
private:
void foo(int e)
{
std::cout << "foo function\n";
std::cout << "e = " << e << std::endl;
}
};
int main(void)
{
Derived derived;
derived.run(10);
return 0;
}
And it does not work, as you can see from the output below
Output:
foo function
e = 679096448
Also, Is that legal in C++? Or I have a piece of code that happens to work? I'm asking that because I'm making a state machine model and I wouldn't like surprises in the middle of development down the line...
EDIT: Adding one more question, Why does the function is called, but the argument value is unspecified?
I think you should use std::function and learn how to use lambdas or std::bind -- I prefer lambdas.
You can define something like this:
#include <functional>
using Comparator = std::function<bool(const MyObject &)>;
And then you can assign a lot of different things to it. You can define either free or static methods. You can use std::bind in order to assign non-static methods. Or you can do what I do, and use a lambda.
void foo(Comparator f) {
if (f(someObject)) {
....
}
}
That part is easy. You can call that like this (for instance):
foo([](const Myobject &obj) { return obj.foo == bar; });
Or:
foo([](const Myobject &obj) { return obj.gleep(); });
Function pointers are a C-ism. std::function is how to do it in C++. It's more powerful. It can do everything a function pointer can do, but it can go further.
I don't care for std::bind, but you can find examples. Lambda syntax isn't necessarily simple, either, but I prefer it.
For one class I want to store some function pointers to member functions of another class. I am trying to return a class member function pointer. Is it possibile?
class one{
public:
void x();
void y();
};
typedef void(one::*PF)(void);
class two :public one{
public:
virtual PF getOneMethodPointer();
};
class three : public two{
std::vector<PF> pointer_to_function;
PF getOneMethodPointer();
pointer_to_function.push_back(getOneMethodPointer())? //how to get method x from class one?
};
In C++11/14, you can always use std::function wrapper to avoid writing unreadable and old C-style function pointers. Here's a simple program with this approach:
#include <iostream>
#include <functional>
using namespace std;
class one {
public:
void x() { cout << "X called" << endl; }
function<void()> getOneMethodPointer();
};
class two : public one {
public:
function<void()> getOneMethodPointer() {
return bind(&one::x, this);
}
};
int main()
{
two* t = new two();
t->getOneMethodPointer()();
delete t;
return 0;
}
As you can see, there's also std::bind used to bind method with std::function. First argument is a reference to the x() method and the second one specifies to which concrete (instantiated) object the pointer is meant to point. Note, that if you say to the st::bind "hey, bind me x() method from one class", it still doesn't know where it is. It knows that - for instance - x() method in this object can be found 20 bytes next to its beginning. Only when you add that it is from for example two* t; object, the std::bind is able to locate the method.
EDIT: Answering to your questions in comments: below code shows an example with virtual getMethodPointer() method:
#include <iostream>
#include <functional>
using namespace std;
class one {
public:
void x() { cout << "X called (bound in one class)" << endl; }
void y() { cout << "Y called (bound in two class)" << endl; }
virtual function<void()> getMethodPointer() {
return bind(&one::x, this);
}
};
class two : public one {
public:
virtual function<void()> getMethodPointer() {
return bind(&one::y, this);
}
};
int main()
{
one* t_one = new one();
one* t_two = new two();
t_one->getMethodPointer()();
t_two->getMethodPointer()();
delete t_one;
delete t_two;
return 0;
}
The C++ syntax for it is this:
class two: public one{
virtual PF getOneMethodPointer(){
return &one::x;
}
};
[Live example]
I would like to pose the following design pattern to discussion. It implements a universal "getMany" method in the base class that uses a given get-method from the derived class to get many entities (here for simplification of explicit type int). This means that any derived class has to inform the "getMany" method which get-method to use. This is a case of calling a pointer-to-member function in a derived class from the base class.
What I would like to pose to discussion: what alternative, easier patterns to achieve the same can you think of?
Thank you!
P.S.: As noted above in a real case one would of course abstract the fixed type "int" to a template type T.
P.P.S.: predefining the get-methods as virtual methods in the base class did not seem a good option, since it would restrict the number of and naming of the get-methods.
#include <iostream>
#include <memory>
#include <algorithm>
#include <vector>
using namespace std;
// EXAMPLE FOR CALL VIA POINTER TO OVERLOADED METHOD IN DERIVED CLASS FROM BASE CLASS
class FooBase
{
public:
template<class PCLASS>
std::vector<int> getMany(int (PCLASS::*getEnt)(int) const, int n, const PCLASS *pClass) const
{
std::vector<int> e;
int i = 0;
e.resize(n);
for (std::vector<int>::iterator it = e.begin(); it!=e.end(); ++it) {
*it = (pClass->*getEnt)( i++ );
}
return e;
};
};
class Foo : public FooBase
{
public:
int Moo(int a) const
{
return a;
};
int Moo(char a) const
{
return (int)a;
};
std::vector<int> Moos(int n) const
{
int (Foo::*f)(int)const;
f = &Foo::Moo;
return getMany<Foo>(f, n, this);
};
};
int main(int argc, char **args)
{
Foo* myFoo = new Foo();
std::vector<int> res = myFoo->Moos(10);
for (std::vector<int>::iterator it = res.begin(); it!=res.end(); ++it) {
std::cout << *it;
}
return 1;
}
Here is an example using function objects.
class FooBase
{
public:
template< typename FunctorType >
std::vector<int> getMany(FunctorType const & functor, int n)
{
std::vector<int> e;
int i = 0;
e.resize(n);
for (std::vector<int>::iterator it = e.begin(); it!=e.end(); ++it) {
*it = functor( i++ );
}
}
};
With this, client code can call getMany using lambdas (c++11) or create their own function objects.
auto callMoo = [this] (char i) { return Moo(i); };
getMany(callMoo, n);
You can just use regular polymorphism here. Have an abstract method that you call via the this pointer from the base class method. A simplified example is:
class FooBase
{
public:
virtual void abstractCall() = 0; // pure virtual function, could make protected if you wanted
void baseMethod()
{
this->abstractCall(); // MUST use the this pointer, not a "." call
}
};
class FooDerived : public FooBase
{
public:
virtual void abstractCall()
{
cout << "my concrete call!" << endl;
}
};
void bar()
{
FooDerived test;
test.baseMethod();
}
I hope that gives you the basic idea.
Is it possible in C++?
For example I have a pointer to a function that takes no parameters and its return type is void:
void (*f)();
and and a function object:
class A
{
public:
void operator()() { cout << "functor\n"; }
};
Is it possible to assign to f the address of an A object? And when I call f() to call the A functor?
I tried this but it doesn't work:
#include <iostream>
using namespace std;
class A
{
public:
void operator()() { cout << "functorA\n"; }
};
int main()
{
A ob;
ob();
void (*f)();
f = &ob;
f(); // Call ob();
return 0;
}
I get C:\Users\iuliuh\QtTests\functor_test\main.cpp:15: error: C2440: '=' : cannot convert from 'A *' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible
Is there any way to achieve this?
You can't do it in the way you've specified, because:
operator() must be a nonstatic function (standards requirement)
a pointer to a non-static function must have an implicit parameter - the pointer to the class instance
your call to f() does not give any indication on which instance of the object A your function is called
Using C++11 and std::function, as Stephane Rolland pointed out, may do the trick - you'll be specifying the pointer to the object in the binding:
std::function<void(void)> f = std::bind(&A::operator(), &ob);
(See question on using std::function on member functions)
If you use C++11, could use std::function
#include <functional>
std::function<void()> f;
int main()
{
A ob;
ob();
f = ob; // f refers to ob
f(); // Call ob();
return 0;
}
Yes it's kind of possible using a C++1/C++0x feature, but to achieve this you should use the std::function which can address to the two types, functions and object functions.
#include <functional>
class A
{
public:
void operator()() { }
};
int main()
{
std::function<void(void)> aFunction;
A ob;
aFunction = ob;
// or as another user said
// aFunction = std::bind(&A:operator(), &ob);
aFunction();
void (*f)();
aFunction = f;
aFunction();
return 0;
}
and if you're stuck with C++03, you could play with std::mem_fun and std::ptr_fun
How about some workaround like this:
Basically you want to have a common way of calling member functions and functions. Then maybe you could create a wrapper that would represent a generic pointer to either a function or member function. Let's say you have Base class and you want to be able to invoke operator() of all derived classes. Then you also have a function() that you want to invoke as well:
class Base
{
public:
virtual void operator()() = 0;
};
class A : public Base
{
public:
void operator()(){ std::cout << "A()" << std::endl; }
};
void function()
{
std::cout << "function" << std::endl;
}
If you create an wrapper that allows you to construct your custom pointer (MyFncPtr):
typedef void (Base::*BaseFncPtr)();
typedef void (*FncPtr)();
class MyFncPtr
{
public:
MyFncPtr(FncPtr f) : fnc(f), baseObj(NULL), baseFnc(NULL) { }
MyFncPtr(BaseFncPtr fPtr, Base* objPtr) : baseFnc(fPtr), baseObj(objPtr), fnc(NULL) { }
void invoke()
{
if (baseObj && baseFnc)
(baseObj->*baseFnc)();
else if (fnc)
fnc();
}
private:
BaseFncPtr baseFnc;
Base* baseObj;
FncPtr fnc;
};
you could achieve it like this:
A a;
MyFncPtr myPtr(&Base::operator(), &a);
myPtr.invoke();
MyFncPtr myPtr2(function);
myPtr2.invoke();
outputs:
A()
function
Hope this helps :)
I didn't know exactly how to title this question. I have a base class and two inheriting classes:
class Base {};
class Action : public Base {};
class Title : public Base {};
Now let's say I have two functions that return either Action * or Title *:
Action *getAction() { return new Action; }
Title *getTitle() { return new Title; }
Is there any way to put these two functions into a map? Like so:
int main()
{
std::map<std::string, Base *(*)()> myMap;
myMap["action"] = getAction;
myMap["title"] = getTitle;
return 0;
}
Right now I'm getting an error:
invalid conversion from `Action* (*)()' to `Base* (*)()'
I could change the signature of the functions to always return the base class, and then it would work, but I'm wondering if there is another way to get around this.
If you use:
Base *getAction() { return static_cast<Base *>(new Action); }
Base *getTitle() { return static_cast<Base *>(new Title); }
then you will not get this error.
std::function is a polymorphic function pointer wrapper provided by the STL.
Of course, using templates, you could write your own function wrapper that stores a target, passes forward arguments and does conversion. This has already been done though, and you should seriously consider carefully before you decide to roll your own. Unless you enjoy reinventing wheels or have very special requirements.
As a proof of concept I have this code:
#include <iostream>
#include <map>
#include <functional>
struct A
{
virtual void f() = 0;
};
struct B : public A
{
void f() { std::cout << "B::f\n"; }
};
struct C : public A
{
void f() { std::cout << "C::f\n"; }
};
B* getB() { return new B; }
C* getC() { return new C; }
int main()
{
std::map<std::string, std::function<A*()>> m;
m["b"] = getB;
m["c"] = getC;
m["b"]()->f();
m["c"]()->f();
}
It leaks memory, but it works.