Solved

bsearch function pointer with C++

Posted on 2000-04-26
7
336 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 55
Visual c++ and text files 7 67
What is sub-make ? 2 58
Installshield for Embarcadero EX 10.1 Berlin 4 39
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 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.

785 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