Solved

bsearch function pointer with C++

Posted on 2000-04-26
7
344 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
Technology Partners: 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

728 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