class 的 C++ 数组函数指针
C++ Array function pointer with class
下面是我的 class 代码:
enum style{BASIC, WEATHER_ONLY};
enum area{AREA0, AREA1, AREA2, AREA3, AREA4, AREA5, NULL_AREA};
enum display{NTP_TIME, TWO_DAY_WEATHER, TREE_DAY_WEATHER, WEEK_WEATHER, TEMPERATURE_AND_HUMIDITY};
typedef struct areaFormat
{
unsigned int x0;
unsigned int y0;
unsigned int width;
unsigned int height;
unsigned int nextSpace;
}AreaFormat;
class DisplayTemplate
{
public:
void NTPTime(AreaFormat range);
void TwoDayWeather(AreaFormat range);
void TreeDayWeather(AreaFormat range);
void WeekWeather(AreaFormat range);
void TemperatureAndHumidity(AreaFormat range);
typedef struct setting
{
AreaFormat areaParameter;
void (DisplayTemplate::*function)(AreaFormat);
}BlockParameter;
void (DisplayTemplate::*Display[7])(AreaFormat) = {&DisplayTemplate::NTPTime, &DisplayTemplate::TwoDayWeather, &DisplayTemplate::TreeDayWeather, &DisplayTemplate::WeekWeather, &DisplayTemplate::TemperatureAndHumidity};
BlockParameter displayStyle[TEMPLATE_MAX_STYLE][DISPLAY_MAX_BLOCK] =
{
{
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=AREA2}, .function=Display[NTP_TIME]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[TWO_DAY_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=AREA3}, .function=Display[TREE_DAY_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[WEEK_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[TEMPERATURE_AND_HUMIDITY]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[NTP_TIME]}
},
{
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=AREA2}, .function=Display[NTP_TIME]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[TWO_DAY_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=AREA3}, .function=Display[TREE_DAY_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[WEEK_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[TEMPERATURE_AND_HUMIDITY]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[NTP_TIME]}
}
};
};
我的 main.cpp :
DisplayTemplate dt;
AreaFormat af;
dt.displayStyle[BASIC][AREA0].function(af);
但是编译报错如下:
错误:必须使用 '.' 或 '->' 来调用 '((DisplayTemplate*)this)->DisplayTemplate::Display[0] 中的指向成员函数的指针( ...)',例如'(... ->* ((DisplayTemplate*)this)->DisplayTemplate::Display[0]) (...)
数组中的displayStyle函数等于DisplayTemplate::Display函数,但为什么我不能调用函数?
当您有指向成员函数的指针时调用成员函数的语法是:
(obj.*memberFuncPtr)(args);
所以你应该:
DisplayTemplate dt;
auto ptr = dt.displayStyle[BASIC][AREA0].function;
auto af = dt.displayStyle[BASIC][AREA0].areaParameter;
(dt.*ptr)(af);
如果auto
不清楚,可以为成员函数指针添加别名类型
using FuncMemPtr = void (DisplayTemplate::*)(AreaFormat);
到DisplayTemplate
,然后:
DisplayTemplate::FuncMemPtr ptr = dt.displayStyle[BASIC][AREA1].function;
下面是我的 class 代码:
enum style{BASIC, WEATHER_ONLY};
enum area{AREA0, AREA1, AREA2, AREA3, AREA4, AREA5, NULL_AREA};
enum display{NTP_TIME, TWO_DAY_WEATHER, TREE_DAY_WEATHER, WEEK_WEATHER, TEMPERATURE_AND_HUMIDITY};
typedef struct areaFormat
{
unsigned int x0;
unsigned int y0;
unsigned int width;
unsigned int height;
unsigned int nextSpace;
}AreaFormat;
class DisplayTemplate
{
public:
void NTPTime(AreaFormat range);
void TwoDayWeather(AreaFormat range);
void TreeDayWeather(AreaFormat range);
void WeekWeather(AreaFormat range);
void TemperatureAndHumidity(AreaFormat range);
typedef struct setting
{
AreaFormat areaParameter;
void (DisplayTemplate::*function)(AreaFormat);
}BlockParameter;
void (DisplayTemplate::*Display[7])(AreaFormat) = {&DisplayTemplate::NTPTime, &DisplayTemplate::TwoDayWeather, &DisplayTemplate::TreeDayWeather, &DisplayTemplate::WeekWeather, &DisplayTemplate::TemperatureAndHumidity};
BlockParameter displayStyle[TEMPLATE_MAX_STYLE][DISPLAY_MAX_BLOCK] =
{
{
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=AREA2}, .function=Display[NTP_TIME]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[TWO_DAY_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=AREA3}, .function=Display[TREE_DAY_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[WEEK_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[TEMPERATURE_AND_HUMIDITY]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[NTP_TIME]}
},
{
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=AREA2}, .function=Display[NTP_TIME]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[TWO_DAY_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=AREA3}, .function=Display[TREE_DAY_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[WEEK_WEATHER]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[TEMPERATURE_AND_HUMIDITY]},
{.areaParameter ={.x0=0, .y0=0, .width=0, .height=0, .nextSpace=NULL_AREA}, .function=Display[NTP_TIME]}
}
};
};
我的 main.cpp :
DisplayTemplate dt;
AreaFormat af;
dt.displayStyle[BASIC][AREA0].function(af);
但是编译报错如下: 错误:必须使用 '.' 或 '->' 来调用 '((DisplayTemplate*)this)->DisplayTemplate::Display[0] 中的指向成员函数的指针( ...)',例如'(... ->* ((DisplayTemplate*)this)->DisplayTemplate::Display[0]) (...)
数组中的displayStyle函数等于DisplayTemplate::Display函数,但为什么我不能调用函数?
当您有指向成员函数的指针时调用成员函数的语法是:
(obj.*memberFuncPtr)(args);
所以你应该:
DisplayTemplate dt;
auto ptr = dt.displayStyle[BASIC][AREA0].function;
auto af = dt.displayStyle[BASIC][AREA0].areaParameter;
(dt.*ptr)(af);
如果auto
不清楚,可以为成员函数指针添加别名类型
using FuncMemPtr = void (DisplayTemplate::*)(AreaFormat);
到DisplayTemplate
,然后:
DisplayTemplate::FuncMemPtr ptr = dt.displayStyle[BASIC][AREA1].function;