Solved

bsearch function pointer with C++

Posted on 2000-04-26
7
332 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 learn how to clear a vector as well as how to detect empty vectors in C++.
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now