passing method pointer as a function pointer

This is a discussion on passing method pointer as a function pointer within the C++ Programming forums, part of the General Programming Boards category; Hi! i have this code: Code: class MainWindow { void load(); [...] void ee_resize_cb (Ecore_Evas * _ee); }; void MainWindow::load() ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    passing method pointer as a function pointer

    Hi!

    i have this code:
    Code:
    class MainWindow
    {
    	void		load();
    [...]
    	void		ee_resize_cb (Ecore_Evas * _ee);
    };
    
    void
    MainWindow::load()
    {
    	[...]
    	ecore_evas_callback_resize_set(ee, ee_resize_cb);
    }
    The protorype for ecore_evas_callback_resize_set() is:
    Code:
    void ecore_evas_callback_resize_set ( Ecore_Evas * ee, void(*)(Ecore_Evas *ee) func)
    The compiler gives error:
    Code:
    main-window.cpp:79: error: argument of type 
    ‘void (MainWindow::)(Ecore_Evas*)’ does not match ‘void (*)(Ecore_Evas*)’
    If I try to pass &ee_resize_cb to the function, i get another error:
    Code:
    main-window.cpp:79: error: ISO C++ forbids taking the address 
    of an unqualified or parenthesized non-static member function to form a pointer to member function.  
    Say ‘&MainWindow::ee_resize_cb’
    How can I pass a method pointer as a function pointer, then?
    Thanks for your help!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    This will get you up-to-speed with function pointers in C/C++: http://www.newty.de/fpt/index.html

    gg

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    For the second error, you must do &MainWindow::ee_resize_cb, as the compile error says. Just doing ee_resize_cb or &ee_resize_cb on a class function is illegal.
    But class function pointers and non-class function pointers use different syntax so be sure to read the link.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Ok, I think I understand the problem now. If I declare ee_resize_cb() as an instance method, it takes an extra hidden argument, the this pointer.

    But what could be the solution? If I declare ee_resize_cb() as a static method, I have no access to the members of MainWindow.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Read the link. It is possible to use class function pointers - they just use a different syntax and require the address of the instance of the class whose function you want to call.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Thanks for the help! I declared ee_resize_cb() as a static method and I made a static pointer which points to this. That tutorial is really elaborated!

    Unfortunately, using multiple instances could make this solution much more complex. But luckily i need only one MainWindow ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Passing pointer into function bug
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2005, 12:13 AM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21