gpt4 book ai didi

c++ - 使用类成员函数作为函数指针?

转载 作者:行者123 更新时间:2023-11-30 02:23:21 25 4
gpt4 key购买 nike

我试图将一个指向类的成员函数的函数指针传递给 std::unique(),但遇到了一些我无法破译的编译器错误。

// So this will otherwise compile
class Vec2 {};
class Polygon {};

// ConvexHull.h:
#include <vector>
#include <algorithm>
class ConvexHull {
public:
// Used to sort an array of vectors so the bottom-right element is first:
bool bottomRight(const Vec2& a, const Vec2& b);
// Used to determine if coordinates are equivalent:
bool vecEqual(const Vec2& a, const Vec2& b);
Polygon chGrahamScan(std::vector<Vec2> points);
};

// ConvexHull.cpp:
bool ConvexHull::bottomRight(const Vec2& a, const Vec2& b) {
// implementation...
return false; // So this will otherwise compile
}

bool ConvexHull::vecEqual(const Vec2& a, const Vec2& b) {
// implementation...
return false; // So this will otherwise compile
}

Polygon ConvexHull::chGrahamScan(std::vector<Vec2> points) {
// Sort according to bottom right:
std::sort(points.begin(),points.end(),bottomRight); // !!!Does not compile!!!
std::vector<Vec2>::iterator it;
// Get rid of duplicate points:
it = std::unique(points.begin(),points.end(),vecEqual); // !!!Does not compile!!!
// rest of implementation...
Polygon poly; // So this will otherwise compile
return poly;
}

int main(){return 0;} // again, to allow this to otherwise compile

忽略两条虚线,这足以编译一些东西。请帮忙!我不确定我在这里做错了什么。我知道这个测试代码说成员函数是公共(public)的,但我的最终目标是让它们成为对用户隐藏但仍按排序和唯一在 ConvexHull::chGrahamScan() 内部使用的私有(private)函数。这些不是我打算使用的仅有的两种比较方法。其他的需要引用由 ConvexHull 类的实例维护的内部状态数据。我最初的解决方案(显然完全有效)有不属于任何类的方法。相反,所有状态数据和比较函数都在全局匿名命名空间中,但这破坏了我项目的线程安全性,所以我决定走这条路。

如果您将其复制并粘贴到一个空项目中,您应该会看到我遇到的错误。它说我需要使用 .* 或 ->* 作为成员函数指针。然而,尝试这样做会给我带来其他错误,说这些函数不能用作成员函数,因为它们属于“未解析的重载函数”类型。

如果事实证明我不能做这样的事情,我将如何最好地实现 std::sort 和 std::unique,使传递给它们的比较函数使用定义之外的状态数据函数本身不会通过使用全局变量违反线程安全?我以前的解决方案做了类似的事情:

namespace {
Vec2 point0; // state data that other comparison functions like
// bottomRight and vecEqual need
bool bottomRight(/*...*/){/*...*/}
bool vecEqual(/*...*/){/*...*/}
}

// Accesses global data. Bad!
Polygon chGrahamScan(std::vector<Vec2> points) {
std::sort(points.begin(),points.end(),bottomRight);
std::vector<Vec2>::iterator it;
it = std::unique(points.begin(),points.end(),vecEqual);
// etc..
}

最佳答案

如果您想将排序函数与比较器一起使用,请将比较器编写为单独的对象,而不是将代码放在您的 ConvexHull 类中。除非您需要 ConvexHull 中的一些私有(private)数据,否则您的比较器不需要是该类的一部分。

class ConvexHull
{
// ... rest of class as shown above ...
private:
// declare friend so comparator can use private data.
friend struct VectorComparator;
};

struct VectorComparator
{
explicit VectorComparator( ConvexHull * hull ) : hull_( hull ) {}
bool operator()( const Vec2& a, const Vec2& b ) const
{
// ... compare the two Vec2 objects here using private data from ConvexHull ...
}
ConvexHull * hull_;
};

然后您可以使用 VectorComparator 作为 std::sort 的参数。

ConvexHull * hull = new ConvexHull;
std::sort(points.begin(),points.end(), VectorComparator(hull) );

您可以为独特的功能制作一个单独的比较器。

struct UniqueVectorComparator
{
bool operator()( const Vec2& a, const Vec2& b ) const
{
// .. compare the two Vec2 objects here.
}
};

it = std::unique(points.begin(),points.end(), UniqueVectorComparator() );

关于c++ - 使用类成员函数作为函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46292196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com