【发布时间】:2016-02-13 20:25:32
【问题描述】:
我正在尝试构建一个具有以方法作为参数的成员函数的类。这些方法在继承的类中定义。我建立了一个最小的例子:
#include <iostream>
struct base
{
base() {}
int number(int (*f)(int))
{
return f(1);
}
};
struct option1 : base
{
int timesTwo(int i){return 2*i;}
option1()
{
std::cout << number(timesTwo);
}
};
struct option2 : base
{
int timesThree(int i){return 3*i;}
int timesFour (int i){return 4*i;}
option2()
{
std::cout << number(timesThree);
}
};
int main()
{
option1 a; //I would expect this to print "2"
}
函数number 中的当前语法适用于一般函数,但我无法让它适用于任何继承类的方法。
【问题讨论】:
标签: c++ inheritance methods