Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

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
0
hvelasco
Asked:
hvelasco
3 Solutions
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now