typedef question

Dear Experts,
 Can somebody explain me what is this:

typedef string (*myfunction)(const int param1, const char param2, const string param3)

Any help will be greatly appreciated.
Thank you
hvelascoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
The above typedef's a function pointer that points to functions which return a string and take three parameters, e.g.

typedef string (*myfunction)(const int param1, const char param2, const string param3);

string foo1(const int param1, const char param2, const string param3) {

stringstream ss;
ss << "Hey, I was called using " << param3.c_str() << " and a this char: " << param2 << " the int was " << param1";

return ss.str();
}

string foo2(const int param1, const char param2, const string param3) {

cout << "Hey, I was called using " << param3.c_str() << " and a this char: " << param2 << " the int was " << param1" << endl;

return string("dummy");
}

myfunction fptr;

string s;

fptr = foo1;

s = fptr ( 42, 'x', string("blubb"));

cout << s.c_str() << endl;

fptr = foo2;

s = fptr ( 22, 'y', string("hey!"));

cout << s.c_str() << endl;

0
jkrCommented:
Ooops, a full sample app would be

#include <string>
#include <iostream>
#include <sstream>
using namespace std;

typedef string (*myfunction)(const int param1, const char param2, const string param3);

string foo1(const int param1, const char param2, const string param3) {

stringstream ss;
ss << "foo1(): Hey, I was called using " << param3.c_str() << " and a this char: " << param2 << " the int was " << param1 << endl;

return ss.str();
}

string foo2(const int param1, const char param2, const string param3) {

cout << "foo2(): Hey, I was called using " << param3.c_str() << " and a this char: " << param2 << " the int was " << param1 << endl;

return string("dummy");
}

int main () {

myfunction fptr;

string s;

fptr = foo1;

s = fptr ( 42, 'x', string("blubb"));

cout << s.c_str() << endl;

fptr = foo2;

s = fptr ( 22, 'y', string("hey!"));

cout << s.c_str() << endl;
}

Output:

foo1(): Hey, I was called using blubb and a this char: x the int was 42

foo2(): Hey, I was called using hey! and a this char: y the int was 22
dummy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
novitiateCommented:
typedef keyword is used to define new data type names to make a program more readable to the programmer.

for example...

#pragma warning(disable:4786)
#include <map>

typedef int* PINT;
typedef PINT* PPINT; //int**

typedef std::map<int, int> IntMap;

int foo()
{
      int a = 0;
      PINT p = &a;

      IntMap m;

      return 0;
}

int main()
{
      return 0;
}

in your case a function pointer type myfunction has been created.

So whenever you need to declare a function pointer for a function of prototype
string f(const int, const char , const string ); you can use myfunction type.

eg.

string foo(const int, const char , const string )
{
   return string("Hello");
}

int main()
{
  myfunction pf = foo; //you are assigning function pointer
  //now you can invoke that function using pf
  cout << (*fp)(0, 0, string("Hello"));
  return 0;
}

you can achieve the same functionality without typdef, but it doesn't look clean.

int main()
{

      string (*fp)(const int, const char, const string) = foo;

      cout << (*fp)(0, 0, string("Hello"));
      return 0;
}

-novi
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

markdocCommented:
Hi. both answers are fairly sufficient but i can give another trivial, yet practical application. Function typedefs are used in windows programming to access functions defined in dynamic link libraries (dll's). For example if we have a function addint that adds two integers compiled in a dll called mydll.dll and the function's prototype is:

int addint(const int, const int)

a typical application would be:

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

typedef int (*addinttype)(const int, const int);   // Define pointer to function.

int main()
{
    HINSTANCE hInst = LoadLibrary("mydll.dll");    // Load the dll.

    if (hInst != NULL)
    {
        // Get function address.
        addinttype addintPtr = (addinttype)GetProcAddress(hInst, "addint");  

        if (addintPtr != NULL)
        {
            int sum = addintPtr(12, 453);   // Use the function pointer.
            cout << "12 + 453 = " << sum << endl;
        }
        else cerr << "Unable to get function address." << endl;
    }
    else cerr << "Unable to load dynamic link library." << endl;

    return EXIT_SUCCESS;
}
0
amar_rCommented:
>>typedef string (*myfunction)(const int param1, const char param2, const string param3)

Creates the type myfunction,for"pointer to function" of arguments returing string, which can be used in contexts like

 myfunction fun1,fun2;

0
amar_rCommented:

 typedef is for creating new data type names.
 
 The declaration  typedef char *String;
 
 makes String a synonym for char  * or character pointer. This can be  declared as
 
 String p;

 p=(String)malloc(5);

 Similarly,  typedef string (*myfunction)(const int param1, const char param2, const string param3)
 
  is Pointer to function returing string of type 'myfunction' .
0
AJ_LennyCommented:
typedef is for creating an alias of a copmlicated type name. You can type in your's program full type name but it would not be as readable to the programmer as using alias of that type. For e.g.:

typedef unsigned char* pUChar;

You could type:     unsigned char* arr;
or write :              pUChar arr;

This is a simply example, but when you will be using much more complicated type you will appreciate aliases.

I won't explain you one more time what does your alias mean, cause you can find it in above posts. If you don't understand how to interpret aliases you should refer to any C/C++ manual.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.