hvelasco
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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;
Creates the type myfunction,for"pointer to function" of arguments returing string, which can be used in contexts like
myfunction fun1,fun2;
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' .
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.
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.
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;