C++ Call Pointer To Member Function

后端 未结 4 1234

I have a list of pointers to member functions but I am having a difficult time trying to call those functions... whats the proper syntax?

typedef void (Box::         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 19:32

    Calling a member function through a pointer to member function has a particular syntax:

    (obj.*pmf)( params );   //  Through an object or reference.
    (ptr->*pmf)( params );  //  Through a pointer.
    

    Although ->* can be overridden, it isn't in the standard library iterators (probably because it would require overrides for every possible function type). So if all you've got is an iterator, you'll have to dereference it and use the first form:

    ((*iter).*pmf)( params );
    

    On the other hand, iterating over a the pointer to members themselves doesn't have this problem:

    (objBox.*(*i))( params );   //  If objBox is an object
    (ptrBox->*(*i))( params );  //  If ptrBox is a pointer
    

    (I don't think you need the parentheses around the *i, but the pointer to member syntax is already special enough.)

提交回复
热议问题