C++,成员函数作为模板参数传递
C++ , Member function passed as template argument
我想传递函数 eval2(T c, T &d),它是类 Algo1 中的成员函数
Algo1.h
#ifndef ALGO1_H
#define ALGO1_H
#include "Algo2.h"
template <typename T>
class Algo1
{
private: T a, b;
public:
Algo1() : a(0), b(0) {}
Algo1(T a_, T b_) : a(a_), b(b_) {}
void anal(T &c);
void eval1(T c);
void eval2(T c, T &d);
friend void get(Algo1 &al, T &a, T &b);
};
#endif
作为 anal(T &c) 函数中的模板参数。
Algo1.hpp
#ifndef ALGO1_HPP
#define ALGO1_HPP
template <typename T>
void Algo1<T>::anal(T &c) {
Algo2<T>::process(eval2<T>, b, c);} //Pass the member function, wrong
template <typename T>
void Algo1<T>::eval1(T c) { a += c; }
template <typename T>
void Algo1<T>::eval2(T c, T &d) { d = a + b + c;}
#endif
在实践中,eval2() 表示一些处理成员数据的成本函数。包含方法 process() 的 "destination" 类看起来像
Algo2.h
#ifndef ALGO2_H
#define ALGO2_H
template <typename T>
class Algo2
{
public:
template <typename Function>
static void process(Function f, T &x, T &res);
};
#endif
Algo2.hpp
#ifndef ALGO2_HPP
#define ALGO2_HPP
template <typename T>
template <typename Function>
void Algo2<T>::process(Function f, T &x, T &res) { f(x, res); } //Call passed function as static
#endif
遗憾的是,eval2(T c, T &d) 是处理成员数据的成员函数,它不能声明为静态的。但是,在类之外,如果没有对象就无法调用它。因此,函数 process() 无法将传递的函数作为静态函数调用。为了解决问题并提取数据,声明并定义了友元函数get(Algo1 &al, T &a, T &b)
template <typename T>
inline void get(Algo1 <T> &al, T &a, T &b )
{
a = al.a;
b = a1.b;
}
它被"内置"到非成员函数 eval3() 中
template <typename T>
inline void eval3(T c, T &d)
{
Algo1 <T> alg;
T a, b;
get(alg, a, b);
}
函数 anal() 被改进为调用 eval3 而不是 eval 2 的形式为
template <typename T>
void Algo1<T>::anal(T &c)
{
Algo2<T>::process(eval3<T>, b, c); //Pass the function OK
}
我有两个问题:
有没有更舒适的方法来传递成员函数,同时保持调用为静态?
在哪里声明和定义 get() 和 eval3() 函数以避免错误
错误1 错误 LNK2019:函数"void __cdecl eval3(double,double &)"中引用的未解析外部符号"void __cdecl get(类 Algo1 &,double &,double &)"(?get@@YAXAAV?$Algo 1@N@@AAN1@Z)
非常感谢您的帮助。
_____________评论________________
第二点已经解决。而不是声明
friend void get(Algo1 &al, T &a, T &b);
需要这样声明
template <typename T>
friend void get(Algo1 &al, T &a, T &b);
- 有没有更舒适的方法来传递成员函数,同时保持调用为静态?
使用 C++11 和 lambda,您可以这样做
template <typename T>
void Algo1<T>::anal(T &c) {
Algo2<T>::process([=](T a1, T& a2) { return this->eval2(a1, a2); }, this->b, c);
}
之前,您必须手动创建函子,如下所示:
template<typename T>
class MyFunctor
{
public:
explicit(Algo1<T>* algo1) : algo1(algo1) {}
void operator () (T a1, T& a2) const
{
algo1->eval2(a1, a2);
}
private:
Algo1<T>* algo1;
};
然后:
template <typename T>
void Algo1<T>::anal(T &c) {
Algo2<T>::process(MyFunctor<T>(this), this->b, c);
}
中作为友元函数(无模板)的类中的get
函数原型和作为模板函数的定义原型不匹配。因此,未解析的外部链接器可能正在搜索友元获取函数的定义。
- 使用指向成员的指针将成员函数作为参数传递
- 将成员函数指针作为参数传递给模板方法
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- 移动类的成员作为常量引用参数传递
- C++将指向成员的指针作为模板参数传递
- C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板专用化
- 将私有数据成员作为默认参数传递给该类的公共方法时出错
- 如何将成员函数作为参数传递?
- 使用 C++11 将指向成员函数的指针作为参数传递
- 是否可以将结构的成员变量作为参数传递
- 将参数传递给构造函数和成员函数时移动或复制
- 如何将已实现的虚拟成员函数作为参数传递
- 将派生类构造函数参数传递给受保护的成员
- 如何通过引用将参数传递给模板类的成员函数?
- 将参数传递给成员函数,就像使用 std::cout 一样
- 通过作为类的公共成员的两个函数将函数作为参数传递
- 使用可变模板参数传递成员函数指针是不明确的
- C++11通过推导的类型参数传递成员函数调用