Reference to Array of Function Pointer.

This tip submitted by Eddy Wu on 2013-02-06 11:20:38. It has been viewed 8399 times.
Rating of 7.1 with 32 votes



#include
#include

typedef void(*fsay)(const char* s);

void Say(const char* s) {
std::cout << s << std::endl;
}


void SayWords(fsay*& rwords, const char*s)
{
for(unsigned i = 0; i < sizeof(rwords); i++) rwords[i](s);
}

using namespace std;

int main(int argc, char *argv[])
{
fsay arwords[3] = { Say, Say, Say}; // an Array of 3 FuncPtr.

fsay* pfsay = arwords;

SayWords(pfsay," Hello World.. ");

pfsay = arwords; /* make it point to
start of arwords
again.
*/

delete[]pfsay; /* release mem arwords
ocuppied. */

pfsay = 0;

system("PAUSE");
return EXIT_SUCCESS;
}



/*

This is to demonstrate a Reference to FuncPtr, the important factor I want to stress here is about the Reference to Pointer Variable, remember because a Reference Var should not refer to a NULL or Nothing Else on the Heap pointed to by a Pointer it refers to, you should remember that All Reference to Pointer variable should be pass to the parameter local to the function call, so when the function return it's lifetime would goes out of scope and be destroy automatically. I compile & run it success with the Dev-C++ 4.9.9.2, but only in a few seconds the system pops up a MessageBox showing that some unpredicted thing had been encountered and feel sorry to have to close immediately. Hope you can fix that bug and share back to me.. : - )

*/





More tips

Help your fellow programmers! Add a tip!