Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question How to call a class member function via function pointer in map [C++]

    I'm trying to declare a std::map that is keyed by a char and has a function pointer as a second parameter, these functions would be a member function of my class. But for some odd reason I cannot call the functions when extracted...

    So, I have a class A as shown below, it contains a map as well as a function

    Code:
    class A
    {
    private:
    	map<char, void (A::*)(void)> mapA;
    public:
    	void func();
    Next I have main code that looks like this:
    Code:
    A::A()	// constructor
    {
    	// Generate User Options
    	mapA['a'] = &A::func;
    }
    Now, I try to execute func by extracting it from the map and running the function:
    Code:
    	map<char, void (A::*)(void)>::iterator it;
    	for ( it=mapA.begin() ; it != mapA.end(); it++ )
    	{
    		(*it).second();
    	}
    I would assume this would launch function "func" as it is stored in the map, but instead it generates the following error message:
    error C2064: term does not evaluate to a function taking 0 arguments

    Any help would be greatly appreciated...
    Thanks,

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    That error message isn't very helpful.

    The problem is that, while you can store pointers to member functions in variables, if you want to call them again you must have an instance of the class they are defined in handy to use with them, otherwise if they used 'this' or accessed any other member data the behavior would be undefined, because there wouldn't be any.

    So, use static functions if you can, and if you can't do that try storing objects that define an operator() instead of functions. If you can't do that either you'll need some ugly syntax like (some_instance_of_A.*(it->second))

    Short example of the options:
    Code:
    class Test
    {
    public:
    	void bar() { std::cout << "bar"; }
    	static void baz() { std::cout << "baz"; }
    	void operator()() { std::cout << "foo"; }
    };
    
    int main()
    {
    	Test a;
    	void (Test::*member)() = &Test::bar;
    
    	//member(); // error: must use ‘.*’ or ‘->*’ to call pointer-to-member function
    	
    	(a.*member)(); // Works fine with a specific instance. A bit ugly, though
    
    	void (*staticfn)() = &Test::baz;
    	staticfn(); // Fine, static members don't need context
    
    	a(); // Fine, obviously. functionoid-style call
    }
    C++ faq lite on the subject, highly recommended


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •