Solved

bsearch function pointer with C++

Posted on 2000-04-26
7
338 Views
Last Modified: 2008-02-20
i *think* this is an easy question, just haven't figured out how to go about it.

i want to do a c-style bsearch (unless there is a better way) on an array and the compare function (5th param to bsearch) takes a function pointer.

well, my comparison function is inside the object that contains this search.  and i need to pass that function pointer to bsearch(), however, i get compiler errors.

can i pass a member function pointer to bsearch?  or how do i do this with c++?
0
Comment
Question by:Billy_Pilgrim
7 Comments
 
LVL 2

Author Comment

by:Billy_Pilgrim
ID: 2751912
forgot to mention:  ANSI only, please.  this app runs on a couple of different platforms. . .
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2752008
Best way , i think, write you oun bsearch as in next text (from
http://www.snippets.org/deque.C)

static int Q_BSearch( queue *q, void *key,int (*Comp)(const void *, const void*))
{
      int low, mid, hi, val;

      low = 0;
      hi = q->size - 1;

      while (low <= hi)
      {
            mid = (low + hi) / 2;
            val = Comp(key, index[ mid ]);

            if (val < 0)
                  hi = mid - 1;

            else if (val > 0)
                  low = mid + 1;

            else /* Success */
                  return mid;
      }

      /* Not Found */

      return -1;
}
0
 
LVL 2

Author Comment

by:Billy_Pilgrim
ID: 2752047
AlexV. . . thanks but doesn't solve the problem.

if i say:

class test {
public:
  void testit();
  int compare(void*, void*);
}

void::testit()
{
  val = bsearch(key, base, width, #elements, this->compare);
}

i get a compiler error, because in fact i don't think you can pass "this->compare" to bsearch.  

i do have source for bsearch so i may modify it slightly to call instead of a function pointer, a virtual member function for the comparison.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 7

Expert Comment

by:KangaRoo
ID: 2752166
As you thought, you can not use a pointer to member as an ordinary function pointer. If you make the compare function static, the problem should be solved. This works only if the compare function does not use any non-static object members.
Another option is to use STL's search function, which can take a functor (an object with an operator() defined).
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2752331
You can make bsearch member of class as
class test {
public:
  void testit();
  int compare(void*, void*);
  int bsearch(void *key, ...);
}
void::testit()
{
  val = bsearch(key, base, width, #elements, compare);
}
It must work!

 


0
 

Accepted Solution

by:
Samir040498 earned 50 total points
ID: 2753330
Try to make the function static.... you can then pass the address of the function as test::compareFunction to bsearch ...
0
 
LVL 2

Author Comment

by:Billy_Pilgrim
ID: 2770118
thank you, samir!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

839 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