C++ 中堆类的多态性;
假设我有一个像这样的 Rectangle 类:
class Rectangle {
public:
double width;
double height;
}
现在我希望将两个可能不同的 Rectangles 列表存储为堆,但第一个堆应该基于 宽度
,第二个高度
。另外,我希望使用stl的make_heap
函数来堆化。理想情况下,我应该能够在堆上调用 .heapify()
,并且根据它所属的类,相关堆应该通过将正确的比较函数传递给 来堆化自身make_heap,也许使用动态调度。我所拥有的是以下内容:
class Heap {
public:
vector<Rectangle> data;
virtual bool comp(Rectangle a, Rectangle b);
void heapify() { make_heap(data.begin(), data.end(), comp); }
// other methods that make use of comp directly
}
class WidthHeap : public Heap {
bool comp(Rectangle a, Rectangle b); // compares by width
}
class HeightHeap : public Heap {
bool comp(Rectangle a, Rectangle b); // compares by height
}
这都是错误的,因为我想我只是不理解 C++ 中的函数,这就是为什么我需要你的帮助。
Let's say I have a Rectangle
class like so:
class Rectangle {
public:
double width;
double height;
}
Now I wish to store two possibly different lists of Rectangles
as heaps, except that the first heap should be based on width
and the second on height
. Also, I wish to use the stl's make_heap
function to heapify. Ideally, I should be able to call .heapify()
on a heap, and based on the class that it belongs to, the heap in question should heapify itself by passing the correct comparison function to make_heap
, perhaps using dynamic dispatch. What I have is the following:
class Heap {
public:
vector<Rectangle> data;
virtual bool comp(Rectangle a, Rectangle b);
void heapify() { make_heap(data.begin(), data.end(), comp); }
// other methods that make use of comp directly
}
class WidthHeap : public Heap {
bool comp(Rectangle a, Rectangle b); // compares by width
}
class HeightHeap : public Heap {
bool comp(Rectangle a, Rectangle b); // compares by height
}
This is all wrong because I guess I just don't understand functions in C++, which is why I would like your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为
comp
是一个成员函数指针,所以如果不传递this
就不能调用它。您需要将this
绑定到它:std::bind
可以在 C++11 的
标头中找到,并且也可以使用 TR1 在
中作为std::tr1::bind
使用。如果您无法使用 TR1 或 C++11,可以使用 Boost 库。演示:http://ideone.com/5zhmg
Because
comp
is a member function pointer, it cannot be called without passingthis
. You need to bindthis
to it:std::bind
can be found in the<functional>
header in C++11, and also available asstd::tr1::bind
in<tr1/functional>
using TR1. If you can't use TR1 or C++11, there's a Boost library for it.Demo: http://ideone.com/5zhmg
除了绑定之外,另一种解决方案是简单地要求将函数指针传递给
Heap
的构造函数。然后:
有趣的是,您甚至可以更进一步,只需一点想象力,实际上可以同时保留两个订单。
虽然……这可能有点过分了;)
Apart from binding, another solution is simply to ask for a pointer to function to be passed to the constructor of
Heap
.And then:
What is interesting, is that you can even go further, and actually keep both orders at once with a little imagination.
Although... it's probably a little overkill ;)
您正在传递一个成员函数。成员函数有一个 this 指针。
make_heap
函数应该从哪里获取它?具体来说,使用以下语法调用指向成员函数的指针(对于发明的类
X
):obj_ptr
就是make_heap
函数丢失的。您需要以某种方式提供它,最简单的方法可能是通过 C++11 中的 lambda:或者甚至可能是
std::bind
:如果您无权访问这些 C++11功能,请使用 Boost.Bind。
You are passing a member function. Member functions have a
this
pointer. Where should themake_heap
function get that from?Specifically, pointer to member functions are invoked with the following syntax (for an invented class
X
):The
obj_ptr
is what themake_heap
function would be missing. You somehow need to provide it, and the easiest way is probably through a lambda in C++11:Or maybe even
std::bind
:If you don't have access to these C++11 functionalities, please resort to Boost.Bind.