How to call through a member function pointer?
More parentheses are required:
(bigCat.*pcat)();
^ ^
The function call (()
) has higher precedence than the pointer-to-member binding operator (.*
). The unary operators have higher precedence than the binary operators.
Calling a function pointer on a member function
myMethod
is a member function, so you need to call it on an instance of the class it is a member of. Since you are already in a member function, you can use this
:
(this->*myMethod)()
How can i call a member function pointer from another member function?
You need to specify which object to call the function on:
(this->*fnPointer)();
Calling C++ member functions via a function pointer
Read this for detail :
// 1 define a function pointer and initialize to NULL
int (TMyClass::*pt2ConstMember)(float, char, char) const = NULL;
// C++
class TMyClass
{
public:
int DoIt(float a, char b, char c){ cout << "TMyClass::DoIt"<< endl; return a+b+c;};
int DoMore(float a, char b, char c) const
{ cout << "TMyClass::DoMore" << endl; return a-b+c; };
/* more of TMyClass */
};
pt2ConstMember = &TMyClass::DoIt; // note: <pt2Member> may also legally point to &DoMore
// Calling Function using Function Pointer
(*this.*pt2ConstMember)(12, 'a', 'b');
How to call through pointer-to-member function saved in a container?
Because of the priority binding for the ()
operator, you need to add some more parens:
result = (this->*(*iter))();
How to call member function through member function pointer?
(this->*f1)() + (this->*f2)();
Regardless of whether you're calling it from inside the class, you have to explicitly specify the object on which to call (in this case this
). Also note the required parentheses. The following is wrong:
this->*f1() + this->*f2()
C++ Call Pointer To Member Function
Pointers to non-static member functions are a unique beast with unique calling syntax.
Calling those functions require you to supply not just named parameters, but also a this
pointer, so you must have the Box
pointer handy that will be used as this
.
(box->*h)(xPos, yPos, width, height);
Use pointer to member function to determine which function to call
Syntax to call a member function via member function pointer is
(this->*memf)();
You cannot magically turn the string into a member function pointer. Sloppy speaking, names of functions do not exist at runtime. If you want such mapping you need to provide it yourself. No way around that. What you can avoid is the "forest of if-else" by using a std::unordered_map
:
#include <unordered_map>
#include <string>
#include <iostream>
class Karen
{
public:
void complain(std::string level) {
static const std::unordered_map<std::string, void(Karen::*)() const> m{
{"debug",&Karen::debug},
{"info",&Karen::info},
{"warning",&Karen::warning},
{"error",&Karen::error}
};
auto it = m.find(level);
if (it == m.end()) return;
(this->*(it->second))();
}
private:
void debug(void) const { std::cout << "debug\n"; }
void info(void) const { std::cout << "info\n"; }
void warning(void) const { std::cout << "warning\n"; }
void error(void) const { std::cout << "error\n"; }
};
int main() {
Karen k;
k.complain("info");
}
Live Demo
As mentioned in comments, you could use an enum in place of the string. When possible you should use the help of the compiler, which can diagnose a typo in an enum but not in a string. Alternatively you could directly pass a member function pointer to complain
. Then implementation of complain
would be trivial, no branching needed. Though this would require the methods to be public and the caller would have to deal with member function pointers.
If you are not allowed to use C++11 or newer you should have a serious talk with your teacher. Soon C++20 will be the de facto standard and things have changed quite a lot. I am not fluent in C++98 anymore, so here is just a quick fix of the above to get it working somehow. You cannot use std::unordered_map
but there is std::map
and initialization of the map is rather cumbersome:
#include <map>
#include <string>
#include <iostream>
class Karen
{
typedef void(Karen::*memf_t)() const;
typedef std::map<std::string,void(Karen::*)() const> map_t;
public:
void complain(std::string level) {
map_t::const_iterator it = get_map().find(level);
if (it == get_map().end()) return;
(this->*(it->second))();
}
private:
const map_t& get_map(){
static const map_t m = construct_map();
return m;
}
const map_t construct_map() {
map_t m;
m["debug"] = &Karen::debug;
m["info"] = &Karen::info;
m["warning"] = &Karen::warning;
m["error"] = &Karen::error;
return m;
}
void debug(void) const { std::cout << "debug\n"; }
void info(void) const { std::cout << "info\n"; }
void warning(void) const { std::cout << "warning\n"; }
void error(void) const { std::cout << "error\n"; }
};
int main() {
Karen k;
k.complain("info");
}
Live Demo
Related Topics
What Are Access Specifiers? Should I Inherit With Private, Protected or Public
Optimizing Away a "While(1);" in C++0X
Inline Functions VS Preprocessor Macros
Why Is It Wrong to Use Std::Auto_Ptr≪≫ With Standard Containers
How to Use Cout ≪≪ Myclass
Calling Class Method Through Null Class Pointer
Enum to String in Modern C++11/C++14/C++17 and Future C++20
How to Remove Unused C/C++ Symbols With Gcc and Ld
Finding C++ Static Initialization Order Problems
How to Iterate Over Cin Line by Line in C++
Why Is There No Call to the Constructor
Significance of Ios_Base::Sync_With_Stdio(False); Cin.Tie(Null);
Is There a Simple Way to Convert C++ Enum to String
Adding External Library into Qt Creator Project
Reinterpret_Cast Creating a Trivially Default-Constructible Object
What Happens to a Detached Thread When Main() Exits
Does "&S[0]" Point to Contiguous Characters in a Std::String