Below is my class code :
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]}
}
};
};
My main.cpp :
DisplayTemplate dt;
AreaFormat af;
dt.displayStyle[BASIC][AREA0].function(af);
But compile show error as below : error: must use '.' or '->' to call pointer-to-member function in '((DisplayTemplate*)this)->DisplayTemplate::Display[0] (...)', e.g. '(... ->* ((DisplayTemplate*)this)->DisplayTemplate::Display[0]) (...)
In array displayStyle function is equal DisplayTemplate::Display function, but why i can't call function?
CodePudding user response:
Syntax to call a member function when you have a pointer to member function is:
(obj.*memberFuncPtr)(args);
so you should:
DisplayTemplate dt;
auto ptr = dt.displayStyle[BASIC][AREA0].function;
auto af = dt.displayStyle[BASIC][AREA0].areaParameter;
(dt.*ptr)(af);
If auto
is not clear, you can add alias type for member function pointer
using FuncMemPtr = void (DisplayTemplate::*)(AreaFormat);
to DisplayTemplate
, then:
DisplayTemplate::FuncMemPtr ptr = dt.displayStyle[BASIC][AREA1].function;