comparing function pointers [was: typedef problem]


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: comparing function pointers [was: typedef problem]

  1. #1
    Join Date
    May 2005
    Posts
    58

    Question comparing function pointers [was: typedef problem]

    i want to declare function pointer for the functions which takes no arguments and returns int...
    so i am using typedef
    typedef int (*function_type)();

    map (dictionary) contains name and the function pointer for function which gets value of name
    using lookup function i am searching "name" in the dictionary and corrosponding function pointer and then function pointer will help in calling a function

    function_type function = lookup("name",dictionary);
    lookup function will call function which returns int using function pointer....

    then i am using following function to lookup for the value
    function_type Lookup(string target, map<string, function_type>& dictionary)
    {
    map<string, function_type>::iterator iter = dictionary.find(target);
    if (iter == dictionary.end())
    // some error handling
    else
    return iter->second;
    }


    now i have one int value given by user and i want to comapre it with value return by lookup function....but lookup function will return function_type value..so how can i compare???
    type compatiblity will not be ther so how can do the comparision?

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Teh question is: what does the int represent? if it's an arbitrary ID (not the actual address), then you need another map. Perhaps a your original map shouldn't contain strings as function names but numbers. BTW. wouldn't it be easier for the user to enter the function's name instead of a number?
    Anyway, you have to make a design choice: the map must contain a function address, withou any doubt. The first element in the pair however should be either an int or a string (the name of the function).
    Danny Kalev

  3. #3
    Join Date
    May 2005
    Posts
    58

    Question

    #include <iostream>
    #include <map>
    using namespace std;

    const int Max = 72;
    const int NULLCHAR = '\0';

    typedef char PrpType[Max];
    typedef int (*function_type)();

    /*PrepClass objects each contain a short C-style string.The idea is that such an object contains a line of text
    The class basically provides object-oriented packaging for a C-style string.*/

    class PrpClass
    {
    private:
    PrpType Info;
    public:
    PrpClass(void);
    PrpClass(PrpType Str);
    void GetLine(PrpType Line) const;
    };

    /* Given: Nothing.
    Task: This is the default constructor. It creates a PrpClass object
    containing the empty string.
    Return: Nothing directly, but the implicit object is created.
    */

    PrpClass::PrpClass(void)
    {
    Info[0] = NULLCHAR;
    }

    /* Given: Nothing.
    Task: This constructor creates a PrpClass object containing the
    string Str.
    Return: Nothing directly, but the implicit object is created.
    */
    PrpClass::PrpClass(PrpType Str)
    {
    strcpy(Info, Str);
    }

    /* Given: Nothing.
    Task: To get the string contained in the implicit PrepClass object.
    Return: Line A copy of the string contained in the implicit object.
    */

    void PrpClass::GetLine(PrpType Line) const
    {
    strcpy(Line, Info);
    }

    /* Given: LHS A PrpClass object (the left side of the comparison).
    RHS A PrpClass object (the right side of the comparison).
    Task: To compare the strings containe in LHS and RHS.
    Return: In the function name, it returns true if the LHS string is less
    than the RHS string, false otherwise.
    */
    bool operator<(PrpClass LHS, PrpClass RHS)
    {
    PrpType Left, Right;

    LHS.GetLine(Left);
    RHS.GetLine(Right);

    if (strcmp(Left, Right) < 0)
    return true;
    else
    return false;
    }

    // Function prototypes:
    void LoadData(map<PrpClass, function_type> dictionary;);
    function_type Lookup(PrepType Target, map<PrpClass, function_type> &dictionary;);

    int main(void)
    {
    map<PrpClass, function_type> dictionary;
    PrepType Target;

    LoadData(dictionary);
    function_type function = Lookup(PrepClass("browsterstream"), dictionary);



    }

    /* Given: Target The word to look up.
    Dictionary The map containing the dictionary data.
    Task: To look up Target in the Dictionary map and print the meaning
    of Target if it was found, or a "not found" message otherwise.
    Return: Nothing.
    */


    function_type Lookup(PrpClass target, map<PrpClass, function_type>& dictionary)
    {
    map<PrpClass, function_type>::iterator iter = dictionary.find(target);
    if (iter == dictionary.end())
    // some error handling
    else
    return iter->second;
    }


    /* Given: Dictionary A map.
    Task: To add suitable data to Dictionary.
    Return: Dictionary The updated map.
    */

    void LoadData(map<PrpClass, function_type> &dictionary)
    {
    Dictionary.insert(make_pair(PrpClass("browserstream"), (function_type)(&get_browser_stream));
    }
    THIS IS my actual code...now in this code after getting value from look up function i have to compare it with value(int) given by user....but value given by lookup function is of type..function_type....
    so can i compare int with compare type???
    Last edited by swapnil_paranja; 08-12-2005 at 06:30 AM.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    I still don't understand what the int represents. Is it the address of a function? If so, you can use an explicit cast of the function pointer returned from map::find()->second. Just make sure that sizeof(int) on your platform ==sizeof(function_type).
    Danny Kalev

  5. #5
    Join Date
    May 2005
    Posts
    58
    actually typedef for function returning nothing and taking one argument...

    typedef void (*function_type)(&argument)..

    but i have to use function retruning int and taking no argument..so i am using this declaration...

    typedef int (*function_type)();is it right?
    and then
    return iter->second;
    will call function using function ptr...and get the value from it...and then i want to compare that value with value provided by user...but how sud i do that???is my code right?so that i can write function which will compare these values..

  6. #6
    Join Date
    May 2005
    Posts
    58

    Question

    actually typedef for function returning nothing and taking one argument...

    typedef void (*function_type)(&argument)..

    but i have to use function retruning int and taking no argument..so i am using this declaration...int is a return type..typedef int (*function_type)();is it right?
    and then
    return iter->second;
    will call function using function ptr...and get the value from it...and then i want to compare that value with value provided by user...but how sud i do that???is my code right?so that i can write function which will compare these values..

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    first of all, typdef's must have defined types as parameters and return values. More specifically, if you want to typdef a function that has void as a return value and an argument of a certain type, you must specify that type:

    typedef void (*ptr_vfi)(int);

    typedef int (*ptr_ifv)();

    ptr_vfi pf1 = myfunc;
    ptr_ifv pf2 = anotherfunc;

    you can't compare pf1 and pf2, at least no directly:

    pf1==pf2; //compilation error, incompatible types

    (void *) pf1 == (void*) pf2; //should work on most platforms


    Pointer to functions are strongly typed which means that changing the arguments, their orde, number etc. or the return value -- is invalid. Therefore, you can't compare ptr_f with another function that returns int and takes no arguments, unless you use a brute force cast, as I have shown above.

    Now the real question is: why do you need incompatible function pointers? If the function's signature is different, it's obvious that you're dealing with different functions, hence different addresses, right? so why compare them in the first place? It's like comparing a string with double -- the types are different so what's the point?
    Last edited by Danny; 08-13-2005 at 09:43 AM.
    Danny Kalev

  8. #8
    Join Date
    May 2005
    Posts
    58

    Question

    now my actual problem is..user is going to give me one value(int type) and name of the function....after getting name of the function i want to call that function,which returns int type value and then compare value give by user(int type) with value returned by function(int type)

    there are hunderds of functions already written and i just need to call the function depending upon the name given by user..

    so i have made one map...in which.key is the name of function and value is function pinter...once i get function name from user i will search throgh map and find the function pointer and then execute the function...

    so i am using tyedef for functionpointer....so once i use the function pointer it will return the value of function_type...and i need to copmpare this with value given by user...thats why i wanted to use the type conversion...

    so the approach i am using is right????need your suggestion...

    thank you...

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    OK, so you want to call the function whose address is stored in the map.
    Simply use it as if it were an ordinary function:


    Code:
    int myfunc();
    
    typedef int (*func_ptr)();
    
    func_ptr = myfunc;
    
    int val=func_ptr(); //calls myfunc();
    
    if(val==user_int)
    
    ...
    Remmeber: you're not comparing pointers here, you're usint them to call a function, store its return value and compare it with the user's input.
    Danny Kalev

  10. #10
    Join Date
    May 2005
    Posts
    58
    thank you ..so i have written the function compare() to compare the value given by user and value returned by function ...

    int compare (int user_value)
    {
    /*now this lookup function is going to return the pointer to the value..i.e. function pointer for "browserstream" */

    function_ptr function = Lookup(PrepClass("browsterstream"), dictionary)
    int val = function();
    .....
    ...
    ..... code for comparing value provided by user and value given by function()stored in val;
    }
    code for lookup is:

    function_type Lookup(PrpClass target, map<PrpClass, function_type>& dictionary)
    {
    map<PrpClass, function_type>::iterator iter = dictionary.find(target);
    if (iter == dictionary.end())
    // some error handling
    else
    return iter->second;
    }

    so now is this a correct logic ????

  11. #11
    Join Date
    Nov 2003
    Posts
    4,118
    It looks right, although I have a few comments about const correctness in the lookup function. I'd write it as:

    function_type Lookup(const PrpClass& target, const map<PrpClass, function_type>& dictionary)
    {
    map<PrpClass, function_type>::const_iterator iter = dictionary.find(target);
    if (iter == dictionary.end())
    {
    // some error handling
    return 0; //NULL
    }

    return iter->second;
    }

    Also, it's cutomary to return bool from compare(), not int. Anyway, I believe that your code shoudl work. Please keep us posted on your results. I'm curious...
    Danny Kalev

  12. #12
    Join Date
    May 2005
    Posts
    58

    Question conversion to user defined type..

    i am using following typedef for the wrapping up c-style string

    const int Max = 72;
    typedef char PrpType[Max];

    now wchar_t is c++ data type..and linename is of wchar_t * type.

    wchar_t * linename;

    linename points to string.
    now i want to convert this wchar_t* linename to PrpType name
    how can i do that..?

    suppose i call a function

    getdata(linename)

    and actual definition of the function is
    void getdata(PrpType name)

    then will the conversion take place internally?like it happens for built in data types
    or what needs to be done?

  13. #13
    Join Date
    Nov 2003
    Posts
    4,118
    In short, you want to convert wchar_t to char? Use the

    cin.narrow(L'a', 0); // return 0 if conversion fails

    function to convert a wchar_t character to char. You will have to use it in a loop to convert a complete string. I'm sure the <locale> library has better ways to accomplish this but it's such a complex and unituitive library that few people ever use it.
    Danny Kalev

  14. #14
    Join Date
    May 2005
    Posts
    58

    Question

    1)
    while(linename != NULL)
    {
    cin.narrow(L'a', 0); ......here i want to use variable name..i.e.linname...because wchar_t string will change time to time...
    is it allowed?
    and
    *linename++ ;//i have to use this till the point i reach end of the string....is it correct?}

    there is casting type called as reinterpret_cast is it helpful?
    reinterpret_cast < type-id > ( expression )


    2)i have one more question regarding my earlier post:

    i have implemented the map having function pointer for function taking no parameter and returning the int value....

    int get_secondary_value()

    the typedef is: typedef int (*function_type)();

    but later on i have discovered that there are actually two types of get function which i need to calll in order to get the value....one described above and other is

    const wchar_t *get_line();

    typedef const wchar_t* (*function_ptr);

    so i have decided to make two maps now ..for these two function types(viz returning int value and const wchar_t*);

    once i get propertyname...i will look in first map using dectionary.find(target)
    and if not found then look in the second map...and get the corrosponding function pointer and then execute the get function to get the value....


    so is it right approach..or is there better approach than this?please suggest me the right approach..and use of 2 maps is right or not?

    3)functions having same returntype but different no of arguments can have same typedef?

    typedef int (*function_type)(int);
    and while inserting function in the map...
    Dictionary.insert(make_pair(PrpClass("getLevel"), (function_type)(&get_level));

    now here function is accepting only one int type parameter but if function defination differs where one function accepts only one argument while second function accepts two parameters...
    both functions have same return type.
    ...so will it work..or all the functions accepting only same no of parameters needs to have similar function pointer (typedef)

    so functions having same returntype but different no of arguments can have same typedef?

    thank you!!
    Last edited by swapnil_paranja; 08-22-2005 at 08:39 AM.

  15. #15
    Join Date
    Nov 2003
    Posts
    4,118
    1. no , reinterpret_cast is certainly not going to work here. Either use a non-standard conversion routine (I'm sure that Win32 has such routines) or use a loop to read one wchar_t at a time from your string, convert it to char and append that char to a char string.

    2+3. No, functions that have different parameter lists are incomptaible. You can't compare, assign or call them with an incompatible typedef. You can use two maps but this looks like a kludge. What hapens if a third function is added? And a fourth one? Such design is very brittle. Instead, you can use a class that wraps all function types or simply define wrapper functions that merely delegate the call the one of the two functions you have.
    Danny Kalev

Similar Threads

  1. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 10:09 AM
  2. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 12:18 PM
  3. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 05:21 PM
  4. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 07:53 AM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center