Solved

bsearch function pointer with C++

Posted on 2000-04-26
7
340 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
[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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

730 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