2

假设我有这个类(继承自 std::Vector,这只是一个例子)

#include <vector>

using namespace std;

template <class T>
class C : public vector<T> {

    // I don't want to use static keyword
    void transformation(T i) {
        i *= 100;
    }

    public:   
    void method() {
        for_each(this->begin(), this->end(), transformation);
    }
};

int main() {
    C<double> c;
    for (int i=-3; i<4; ++i) {
        c.push_back(i);
    }

    c.method();
}

如何使用类本身内部的类方法调用 for_each?我知道我可以使用 static 关键字,但是还有什么其他方法可以在不使用 static 的情况下使用函数对象?

我在编译时收到此错误消息:

for_each.cc:21:55: 错误:无法将 'C::transformation' 从类型 'void (C::)(double)' 转换为类型 'void (C::*)(double)' for_each(this-> begin(), this->end(), 转换);

我想我需要在某个地方添加.*或添加,->*但我不知道在哪里以及为什么。

4

3 回答 3

15

C++11绑定解决方案:

std::for_each(this->begin(), this->end(),
      std::bind(&C::transformation, this, std::placeholders::_1));

C++11 lambda解决方案:

std::for_each(this->begin(), this->end(),
      [this] (T& i) { transformation(i); });

C++14通用 lambda解决方案:

std::for_each(this->begin(), this->end(),
      [this] (auto&& i) { transformation(std::forward<decltype(i)>(i)); });

C++98 bind1st+mem_fun解决方案:

std::for_each(this->begin(), this->end(),
      std::bind1st(std::mem_fun(&C::transformation), this));

注意: this->begin()this->end()调用this->仅是因为在 OP 的代码中它们是模板化基类的成员函数。因此,这些名称主要在全局命名空间中进行搜索。任何其他出现this都是强制性的。

于 2014-09-29T11:49:06.303 回答
4

对于初学者,不要从标准容器继承,它们不是为了继承而设计的(没有虚拟析构函数等)。

其次,关于您的问题,这是因为指向成员函数的指针与指向函数的指针不同。原因是成员函数有一个隐藏的第一个参数,它成为this函数中的指针。解决它的最简单方法是制作函数static

另一种解决方案是使用std::bindC++11 自带的函数:

for_each(this->begin(), this->end(),
    std::bind(&C::transformation, this, std::placeholders::_1));

如果您没有C++11(即使您这样标记了您的问题),那么您可能会得到一些与std::mem_funor一起使用的东西std::bind1st

于 2014-09-29T11:38:43.440 回答
0

您需要绑定this指针:

public:   
void method() 
{
    for_each(this->begin(), this->end(), bind(&C::transformation, this, placeholders::_1));
}
于 2014-09-29T11:40:20.157 回答