function-pointers

Is it possible to create a function pointer to the a function's `new` operator/constructor?

允我心安 提交于 2019-12-05 10:55:22
If I were to wanted to parameterize creating an object, I could of course make a function which called new on a particular class and passed out a pointer. I am wondering if it's possible to skip that step and pass a function pointer to the new operator itself. boost::lambda provides function wrappers for new and delete . These can be used to easily convert an new call into a function object. operator new (as well as the other flavours) takes care of allocating memory but does not construct objects. In fact its return type is void* . What constructs an object is a new expression , which is part

Setting up a array of callbacks and trying to use array index as value in callback

这一生的挚爱 提交于 2019-12-05 10:43:35
When I setup an array of callbacks this way I get 20 in the dialog window for all callbacks. I'd like to get the index in the array instead. Is this possible? The function that calls the callback is expecting the callback to have one parameter. I don't control the caller of the callback because it is part of an external library. Any help is appreciated. for (var i = 0; i < 20; i++) { callbackDB[i] = function(data) { alert(i); } } Because i is evaluated when the function is called, you'll need to scope that value of i in a new function execution in order to retain the value you expect. //

Executing Byte Array in Go

末鹿安然 提交于 2019-12-05 10:09:56
I'm trying to execute shellcode within a Go program, similar to how you can do it with other languages. Example 1 - Shellcode in C program Example 2 - http://www.debasish.in/2012/04/execute-shellcode-using-python.html All methods have broadly similar techniques - assign the shellcode to executable memory via the OS specific allocation (mmap, virtualalloc, etc) and then execute the code by creating a function pointer pointing to that location before executing. Here is my horrible hacky example at performing the same thing in Go. The shellcode has manipulations performed on it before being

What is the cloest thing to a function pointer in java? [duplicate]

折月煮酒 提交于 2019-12-05 09:28:11
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What's the nearest substitute for a function pointer in Java? I just have a situation where it would be nice to have the functionality of something similar to function pointers like you might use in c or c++. The best thing I can think of is combining Runnables and launch them in a different thread. Suggestions? 回答1: Until they add closures to java, the thing seems closest to java.lang.reflect.Method . For

Function taking both pointer to member-function and pointer to const member-function

一世执手 提交于 2019-12-05 08:21:37
I have the following code base: template <typename Type> class SomeClass { public: template <typename ReturnType, typename... Params> void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) { auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); } // ... } }; This works when I pass a pointer to a member-function (non-const). However, if I want to pass a pointer to a const member-function, it results in a compile error and I must duplicate the above function to get this code: template <typename Type> class SomeClass

Lua - Execute a Function Stored in a Table

一世执手 提交于 2019-12-05 08:04:41
I was able to store functions into a table. But now I have no idea of how to invoke them. The final table will have about 100 calls, so if possible, I'd like to invoke them as if in a foreach loop. Thanks! Here is how the table was defined: game_level_hints = game_level_hints or {} game_level_hints.levels = {} game_level_hints.levels["level0"] = function() return { [on_scene("scene0")] = { talk("hint0"), talk("hint1"), talk("hint2") }, [on_scene("scene1")] = { talk("hint0"), talk("hint1"), talk("hint2") } } end Aaand the function definitions: function on_scene(sceneId) -- some code return

int(int, int) style template function type syntax

久未见 提交于 2019-12-05 07:53:54
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. bool(int) is the type of the function; bool(*)(int) is the type of the function pointer. In other words, if you define

Calling C++ member function pointer from a struct

社会主义新天地 提交于 2019-12-05 07:27:06
I have found information on calling C++ member function pointers and calling pointers in structs, but I need to call a member function pointer that exists inside of a structure, and I have not been able to get the syntax correct. I have the following snippet inside a method in class MyClass: void MyClass::run() { struct { int (MyClass::*command)(int a, int b); int id; } functionMap[] = { {&MyClass::commandRead, 1}, {&MyClass::commandWrite, 2}, }; (functionMap[0].MyClass::*command)(x, y); } int MyClass::commandRead(int a, int b) { ... } int MyClass::commandWrite(int a, int b) { ... } This gives

Python ctypes and function pointers

▼魔方 西西 提交于 2019-12-05 07:14:31
问题 This is related to my other question, but I felt like I should ask it in a new question. Basically FLAC uses function pointers for callbacks, and to implement callbacks with ctypes, you use CFUNCTYPE to prototype them, and then you use the prototype() function to create them. The problem I have with this is that I figured that I would create my callback function as such (I am not showing the structures that I have recreated, FLAC__Frame is a Structure): write_callback_prototype = CFUNCTYPE(c

C++ member function pointers in class and subclass

佐手、 提交于 2019-12-05 05:34:09
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 from SpecificClass i am not able to add onBar because the event_t type is only defined for the