|
-
June 15th, 2010, 02:49 PM
#19
Re: Working with a database
 Originally Posted by candlemaster
Would it be possible to save a function's name as a variable, and then use that variable to call the function? For example:
Code:
std::string functionVariable = "MyFunction()";
//or
specialvartype functionVariable = MyFunction();
For the first ideea, the answer is no. That's because the name of a function is not a string. From the programmer's perspective it looks like a text but from the compiler's perspective it's just the relative address where the body of the function can be foud. Probably the best definition would be to say that a function name it's a "named address" (do I have too much imagination ?)
During the compilation all of the function names are replaced with their addresses. If you open the generated exe file with a binary editor, you'll find none of the names of your functions. You cannot relay on function names because they'll be all gone in the exe file.
For the second ideea, the answer is yes.
1. Declare a pointer type matching the declaration of your function:
Code:
typedef std::string (*MY_TYPE_PTR)();
2. Declare a pointer variable of that type:
Code:
MY_TYPE_PTR pMyFunc;
3. Assign the address of your function to that pointer variable:
Code:
pMyFunc = MyFunction;
4. Call the function using the pointer variable:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|