function-pointers

Address of function main() in C/C++

☆樱花仙子☆ 提交于 2020-01-11 03:08:08
问题 Is there a way to find out the address of main() in C or C++ ? Since it is itself a function ,would there be an address of it own ? 回答1: C Sure. Simply go ahead and do it. #include <stdio.h> int main(void) { printf("%p\n", &main); } C++ It is not permitted to take main 's address so, for your purposes, there isn't one: [C++11: 3.6.1/3]: The function main shall not be used within a program. [..] However, in GCC you can take the same approach as you would in C, via a compiler extension:

Dereferencing function pointers in C to access CODE memory

谁都会走 提交于 2020-01-11 02:16:33
问题 We are dealing with C here. I'm just had this idea, wondering if it is possible to access the point in memory where a function is stored, say foo and copying the contents of the function to another point in memory. Specifically, I'm trying to get the following to work: #include <stdlib.h> #include <stdio.h> #include <string.h> void foo(){ printf("Hello World"); } int main(){ void (*bar)(void) = malloc(sizeof foo); memcpy(&bar, &foo, sizeof foo); bar(); return 0; } But running it gives a bus

Wrap a function pointer in C++ with variadic template

我们两清 提交于 2020-01-09 19:48:47
问题 The Question I have a number of C++ functions void f() , R g(T a) , S h(U a, V b) and so on. I want to write a template function that accepts f , g , h and so on as a template argument and calls that function. ie I want something like this: template<MagicStuff, WrappedFunction> ReturnType wrapper(MagicallyCorrectParams... params) { extra_processing(); // Extra stuff that the wrapper adds return WrappedFunction(params); } ... wrapper<f>(); // calls f wrapper<g>(T()); // calls g wrapper<h>(U(),

Wrap a function pointer in C++ with variadic template

允我心安 提交于 2020-01-09 19:47:33
问题 The Question I have a number of C++ functions void f() , R g(T a) , S h(U a, V b) and so on. I want to write a template function that accepts f , g , h and so on as a template argument and calls that function. ie I want something like this: template<MagicStuff, WrappedFunction> ReturnType wrapper(MagicallyCorrectParams... params) { extra_processing(); // Extra stuff that the wrapper adds return WrappedFunction(params); } ... wrapper<f>(); // calls f wrapper<g>(T()); // calls g wrapper<h>(U(),

C++ function pointer (class member) to non-static member function

╄→尐↘猪︶ㄣ 提交于 2020-01-08 16:06:55
问题 class Foo { public: Foo() { do_something = &Foo::func_x; } int (Foo::*do_something)(int); // function pointer to class member function void setFunc(bool e) { do_something = e ? &Foo::func_x : &Foo::func_y; } private: int func_x(int m) { return m *= 5; } int func_y(int n) { return n *= 6; } }; int main() { Foo f; f.setFunc(false); return (f.*do_something)(5); // <- Not ok. Compile error. } How can I get this to work? 回答1: The line you want is return (f.*f.do_something)(5); (That compiles -- I

C++ function pointer (class member) to non-static member function

佐手、 提交于 2020-01-08 16:05:24
问题 class Foo { public: Foo() { do_something = &Foo::func_x; } int (Foo::*do_something)(int); // function pointer to class member function void setFunc(bool e) { do_something = e ? &Foo::func_x : &Foo::func_y; } private: int func_x(int m) { return m *= 5; } int func_y(int n) { return n *= 6; } }; int main() { Foo f; f.setFunc(false); return (f.*do_something)(5); // <- Not ok. Compile error. } How can I get this to work? 回答1: The line you want is return (f.*f.do_something)(5); (That compiles -- I

C++ function pointer (class member) to non-static member function

筅森魡賤 提交于 2020-01-08 16:05:12
问题 class Foo { public: Foo() { do_something = &Foo::func_x; } int (Foo::*do_something)(int); // function pointer to class member function void setFunc(bool e) { do_something = e ? &Foo::func_x : &Foo::func_y; } private: int func_x(int m) { return m *= 5; } int func_y(int n) { return n *= 6; } }; int main() { Foo f; f.setFunc(false); return (f.*do_something)(5); // <- Not ok. Compile error. } How can I get this to work? 回答1: The line you want is return (f.*f.do_something)(5); (That compiles -- I

Assigning function to function pointer

不想你离开。 提交于 2020-01-07 06:36:09
问题 I have currently defined a function pointer and to me it seems like the function matches the definition, however I am getting an error: 1 IntelliSense: a value of type "std::string (RSSCrawler:: )(const web::json::value &headlines)" cannot be assigned to an entity of type "std::string ( )(const web::json::value &headlines)" I am not sure what is wrong, but here is my code string(*GetHeadline)(const value&headlines); GetHeadline = Extract; string RSSCrawler::Extract(const value &headlines) {

(Qt) Read access error when attempting to use any function from another widget class on my MainWindow

孤者浪人 提交于 2020-01-06 08:49:11
问题 I'd like to access some functions of a dialog-type widget from my mainwindow.cpp . I've created a simple function in the widget class that returns an int, which looks like this: dialog.h: public: int getRowCountData(); dialog.cpp: int Dialog::getRowCountData() { return ui->tableWidget->rowCount(); } Usage: my mainWindow.h: private: Dialog *dialog; my mainwindow.cpp: dialog = new Dialog; int count = dialog->getRowCountData(); <<<<<--------------this throws a read access error! code: 0xc0000005

Generic member function pointer help

人走茶凉 提交于 2020-01-06 08:46:09
问题 Hey, I've got a question about general member function pointers. I'm trying to achieve something similar to the following question How to define a general member function pointer Essentially what I want to be able to do is to register a member function pointer that accepts a generic Event object as its argument with a specific event type (for example, a KeyboardEvent). Then, in my input management class, what I want to be able to do is whenever the user hits a key, I can create a