function-pointers

C++ Pointer to member function of an UNKNOWN CLASS

落爺英雄遲暮 提交于 2019-12-07 22:58:38
问题 DISCLAIMER I DO NOT USE BOOST OR OTHER LIBRARIES Finally I've learned how PointerToMemberFunction works. This is my example code. #include <iostream> using namespace std; class Foo { public: void foo ( ) { cout << "I'm a foo method\n"; }; }; class Bar { public: void bar ( Foo* fooPtr , void(Foo::*fooFnPtr)() ) { (fooPtr->*fooFnPtr)(); }; }; int main() { Foo* foo = new Foo(); Bar* bar = new Bar(); bar->bar ( foo , &Foo::foo ); return 0; } Now, what the problem is. Bar::bar must be modified

ANSI C: If a function pointer points to executable code does that mean less execution overhead than simply invoking the function? [duplicate]

隐身守侯 提交于 2019-12-07 21:52:16
问题 This question already has answers here : Does Function pointer make the program slow? (8 answers) Closed 2 years ago . We know that using function pointers in C can be quite helpful when used in the proper scenarios (calling a function at runtime vs compile time, making the code more readable, etc.), but there isn't much literature around simple function invocation vs using a function pointer. void foo(void) { printf("hello\n"); } int testFcn(void) { // simple invokation foo(); return 0; } //

Creating a vector of pointer to functions

别说谁变了你拦得住时间么 提交于 2019-12-07 21:14:40
问题 How do I do this in C++? I know how to create a pointer to a function but that requires having a name for that pointer. What I actually need is to somehow create a pointer without naming it. I know the syntax for array of ptr to functions. This might help: out-type (*ptr[size])(parameters...) 回答1: You should really use a boost/std::function<out_type(parameters)> instead. However, to answer the question at hand, you could use a typedef typedef out_type(*typedef_name)(param_types); std::vector

C++ function pointer to a member function - which address does it receive?

元气小坏坏 提交于 2019-12-07 16:05:23
问题 Assuming I have this class: class Shape { public: int value; Shape(int v) : value(v) {}; void draw() { cout << "Drawn the element with id: " << value << endl; } }; and the following code (which works) Shape *myShapeObject = new Shape(22); void (Shape::*drawpntr)(); drawpntr = &Shape::draw; (myShapeObject ->*drawpntr)(); I have a drawpntr function pointer to a void-returning 0-arguments function member of the class Shape. First thing I'd like to ask: drawpntr = &Shape::draw; the function is a

Does TCL have some concept of function pointers?

↘锁芯ラ 提交于 2019-12-07 14:48:06
问题 Working with TCL and I'd like to implement something like the Strategy Pattern. I want to pass in the "strategy" for printing output in a TCL function, so I can easily switch between printing to the screen and printing to a log file. What's the best way to do this in TCL? 回答1: TCL allows you to store the name of a procedure in a variable and then call the procedure using that variable; so proc A { x } { puts $x } set strat A $strat Hello will call the proc A and print out Hello 回答2: In

How to pass a generic function pointer as parameter

坚强是说给别人听的谎言 提交于 2019-12-07 14:47:32
问题 I have several functions that having similiar declarations: int foo(int a); int bar(int a); int test(int a); And the logics of my message handlers are exactly the same: void HandleFoo(int a) { process(a); int ret = foo(a); if (ret) print(a); } void HandleBar(int a) { process(a); int ret = bar(a); if (ret) print(a); } void HandleTest(int a) { process(a); int ret = test(a); if (ret) print(a); } So I am wondering if it is possible to write a general function: void Handle(int a, func_pointer fn)

How can I use a member function pointer in libcurl

杀马特。学长 韩版系。学妹 提交于 2019-12-07 14:32:58
问题 I am using libcurl I have my downloading of files inside of a class, to which I want to see a progress function. I notice I can set a typical function pointer by curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, progress_func3); However, I would like to set it to a function pointer to my class. I can get the code to compile with curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, &MyClass::progress_func3); and the progress_func3 function will get called. The problem is, as soon as it returns

Can a noexcept function still call a function that throws in C++17?

为君一笑 提交于 2019-12-07 13:59:35
问题 In P0012R1, " Make exception specifications be part of the type system ", I see that noexcept is now becoming a part of the function type. I can't tell whether this will prevent noexcept(true) functions from still being able to call noexcept(false) functions. Will the following code still be valid for C++17? void will_throw() noexcept(false){ throw 0; } void will_not_throw() noexcept(true){ will_throw(); } 回答1: According to cppreference: Note that a noexcept specification on a function is not

Can you cast a “pointer to a function pointer” to void*

谁说我不能喝 提交于 2019-12-07 13:02:32
问题 Inspired by comments to my answer here. Is this sequence of steps legal in C standard (C11)? Make an array of function pointers Take a pointer to the first entry and cast that pointer to function pointer to void* Perform pointer arithmetic on that void* Cast it back to pointer to function pointer and dereference it. Or equivalently as code: void foo(void) { ... } void bar(void) { ... } typedef void (*voidfunc)(void); voidfunc array[] = {foo, bar}; // Step 1 void *ptr1 = array; // Step 2 void

null pointer when getting function pointer using boost::function::target

て烟熏妆下的殇ゞ 提交于 2019-12-07 11:22:27
After reading this answer I thought I had a solution. At least the answer there is what I would like to do but I'm having a problem with the implementation. here is an outline of what I am trying to do typedef map<string, double*> myMap; typedef int (*ftwpt)(const char*, const struct stat*, int); typedef boost::function<int(const char*, const struct stat*, int)> MyFTWFunction; int myFunction(const char*, const struct stat*, int, myMap*); int main() { myMap m_map; char tmpdir[] = "/tmp/mytmp"; MyFTWFunction f = boost::bind(myFunction,_1,_2,_3, &m_map); ftwpt* fpt = f.target<ftwpt>(); if (fpt)