Function pointer problem
-
#include #include using namespace std; void Print(const string& value) { cout << value << endl; } void PrintBy (const string& value, void (*printer)(const string& value)) { printer(value); } class Printer { public: void Print(const string& value) { cout << value << endl; } }; int main() { string s = "hi"; Print(s); // why this work? PrintBy(s, &Print); // but this does not work!? Printer p; PrintBy(s, &p.Print); return 0; }
-
#include #include using namespace std; void Print(const string& value) { cout << value << endl; } void PrintBy (const string& value, void (*printer)(const string& value)) { printer(value); } class Printer { public: void Print(const string& value) { cout << value << endl; } }; int main() { string s = "hi"; Print(s); // why this work? PrintBy(s, &Print); // but this does not work!? Printer p; PrintBy(s, &p.Print); return 0; }
What exactly is the problem?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
#include #include using namespace std; void Print(const string& value) { cout << value << endl; } void PrintBy (const string& value, void (*printer)(const string& value)) { printer(value); } class Printer { public: void Print(const string& value) { cout << value << endl; } }; int main() { string s = "hi"; Print(s); // why this work? PrintBy(s, &Print); // but this does not work!? Printer p; PrintBy(s, &p.Print); return 0; }
-
What exactly is the problem?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
he did label the problem, look for the comments ('//')
-
he did label the problem, look for the comments ('//')
"but this does not work!?" is not a useful problem description. It could be a compiler or linker problem. It could be an assertion. It could be an exception. It could be a logic problem.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
"but this does not work!?" is not a useful problem description. It could be a compiler or linker problem. It could be an assertion. It could be an exception. It could be a logic problem.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
It is if you look at the difference between his two statements
-
try using static inside the class. http://www.parashift.com/c++-faq-lite/pointers-to-members.html[^]
good link! ...you may be right, I didn't think about the address being different for non-statics.
-
#include #include using namespace std; void Print(const string& value) { cout << value << endl; } void PrintBy (const string& value, void (*printer)(const string& value)) { printer(value); } class Printer { public: void Print(const string& value) { cout << value << endl; } }; int main() { string s = "hi"; Print(s); // why this work? PrintBy(s, &Print); // but this does not work!? Printer p; PrintBy(s, &p.Print); return 0; }
What do you mean by "it does not work"?... The code as written just does not compile for a simple reason: Your second call to
PrintBy
does not match the signature of the called function. You either declare a second overload for PrintBy taking a proper pointer-to-method of Printer or (much better) you rely on standard library abilities:#include #include template < typename TPrinter >
void PrintBy( std::string const & pString, TPrinter pPrinter )
{
pPrinter(pString);
}// Your stuff ...
int main()
{
::PrintBy( "hi", Print );::Printer p; ::PrintBy( "hi again", std::bind( &::Printer::Print, &p, std::placeholders::\_1 ) );
}