function-pointers

int(int, int) style template function type syntax

老子叫甜甜 提交于 2020-01-02 03:36:08
问题 I remember that when using Boost.Spirit and for the std::function addition to C++0x, you specify the function type by using a syntax that doesn't use pointers, like in defining std::function<bool(int)> fn , whereas you would cast a pointer like (bool(*)(int))fn . Can anyone tell me the name of this new syntax or any references on this, or how to use it? It seems like a polymorphic function type syntax that applies for functors as well, but I don't really know how to use it. 回答1: bool(int) is

C++ member function pointers in class and subclass

偶尔善良 提交于 2020-01-02 03:27:07
问题 I have one base class which holds a map for function pointers like this typedef void (BaseClass::*event_t)(); class BaseClass { protected: std::map<std::string, event_t> events; public: // Example event void onFoo() { // can be added easily to the map } }; Handling this works prefect, but now i want to make BaseClass an abstract base class to derive from like this: class SpecificClass : public BaseClass { public: void onBar() { // this is gonna be difficult! } }; Although i can access the map

C++ member function pointer with different arguments - or is this bad anyway?

天大地大妈咪最大 提交于 2020-01-01 16:26:59
问题 Even though I fear that you will tell me that this topic was covered several time, I dare to ask it, since I was not able to generate a solution. Probably I was just looking for the wrong thing... Assume that I have a function which receives a "mode" from some external function. Depending on the mode, the function will call different member functions of the same object. This works well for me with member function without any argument, but I did not find out how to extend it to members with

Are two function pointers to the same function always equal?

北城余情 提交于 2020-01-01 07:28:05
问题 Does the C++ standard guarantee that two pointers to a function always compare equal? I understand that this will normally be true for non-inline functions. But if there is an inline function and a pointer to the function is created in two separate compilation units, will the linker merge the two instantiations, or is it allowed to emit duplicate functions? If the answer to the above is "they are equal": Does this still hold if there is a common header with an inline function, and both the

How to implement C++ style function pointers in C#?, Without using delegates

对着背影说爱祢 提交于 2020-01-01 05:47:12
问题 I am learning pointers in C# and was curious if one can use C++ style function pointers in C#. Yes, I know C# has its own equivalent concept for Function Pointers(called as delegates). But I just want to know if the same can be achieved using pointers in C#, without using delegates. If using pointers is completely legal in C#(using unsafe option) and pointer semantics is almost similar to C/C++ then in my opinion one should also be able to use C/C++ style function pointers as well. Please

c++ pointers to operators

不问归期 提交于 2020-01-01 04:32:07
问题 I want to write a pointer in c++ (or in c++0x), that will points to a operator of a class lets say A or B. Is there any method to do it? Of course there is a syntax like int (A::*_p) (); but it doesn't solve this problem. I want to make general pointer, not specifying the base class for it - only pointer for "operator function" #include <thread> #include <iostream> using namespace std; class A { public: int operator()() { return 10; } }; class B { public: int operator()() { return 11; } };

Can I use a lambda function or std::function object in place of a function pointer?

☆樱花仙子☆ 提交于 2020-01-01 04:26:05
问题 I've got a library that I need to use that defines the following: typedef void CallbackFunction(const int& i); and has a function to register your callback that looks like: void registerCallback(CallbackFunction* pCallback); Because I'd like to capture the state of several variables to be used in the callback, I can't simply use a plain function. What I'd prefer to use is a lambda function, but the following doesn't compile: auto fCallback = [](const int& i) { cout << i << endl; };

How to recursively dereference pointer (C++03)?

…衆ロ難τιáo~ 提交于 2020-01-01 02:34:25
问题 I'm trying to recursively dereference a pointer in C++. If an object is passed that is not a pointer (this includes smart pointers), I just want to return the object itself, by reference if possible. I have this code: template<typename T> static T &dereference(T &v) { return v; } template<typename T> static const T &dereference(const T &v) { return v; } template<typename T> static T &dereference(T *v) { return dereference(*v); } My code seems to work fine in most cases, but it breaks when

Is There a way to use the Parameter Names from a typedef

一曲冷凌霜 提交于 2019-12-31 05:42:47
问题 So given a typedef that defines a function pointer with parameter names like this: typedef void(*FOO)(const int arg); Is there a way that I can just use this function pointer to define the signature of my function? Obviously this won't work, but I'd like to somehow use the typedef to specify a function signature with a corresponding type: FOO foo { cout << arg << endl; } Again, I know this doesn't work, and is bad syntax. It will just give the error: error: arg was not declared in this scope

Why can I still call a function from a pointer class after I delete it? [duplicate]

百般思念 提交于 2019-12-31 04:31:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: What will happen when I call a member function on a NULL object pointer? #include <iostream> #include <string> using namespace std; class myClass{ private: int *x, *y, *z; public: myClass(); ~myClass(); void display(); void math(int,int,int); }; void myClass::math(int x,int y,int z){ this->x = new int; this->y = new int; this->z = new int; *this->x = x; *this->y = y; *this->z = z; cout << "result: " << (x*y)+z <