?
Solved

typedef question

Posted on 2005-02-24
7
Medium Priority
?
485 Views
Last Modified: 2010-04-01
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
Comment
Question by:hvelasco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13395545
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
 
LVL 86

Accepted Solution

by:
jkr earned 600 total points
ID: 13399578
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
 
LVL 8

Assisted Solution

by:novitiate
novitiate earned 200 total points
ID: 13399683
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.

 
LVL 4

Assisted Solution

by:markdoc
markdoc earned 200 total points
ID: 13401438
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
 
LVL 1

Expert Comment

by:amar_r
ID: 13403396
>>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
 
LVL 1

Expert Comment

by:amar_r
ID: 13409037

 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
 

Expert Comment

by:AJ_Lenny
ID: 13410430
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question