i'm implementing some pointer functions/Callbacks in my code as follow:
typedef WndDyn* (Edit_t)(Point2d* pThis, const EditParams& EditParams);
Edit_t g_Edit_CB{ nullptr };
typedef WndDyn* (*Show_t)(const Point2d* pThis, const EditParams& EditParams);
Show_t g_Show_CB{ nullptr };
// Punkt3d
typedef WndDyn* (*Edit3d_t)(Point3d* pThis, const EditParams& EditParams);
Edit3d_t g_Edit3d_CB{ nullptr };
typedef WndDyn* (*Show3d_t)(const Point3d* pThis, const EditParams& EditParams);
Show3d_t g_Show3d_CB{ nullptr };
.
.
// Vector2d
.
.
After a few moments I realized that I am repeating the pointer function and I am only changing my element which is Point_2d/Point_3d/.... Could someone help me to reduce the functions. I'm thinking about implementing virtual pure functions, but I usually implement them with functions and not pointer functions, so I have no idea how to do it. I would be very grateful if someone could help me. Thank you in advance
CodePudding user response:
Since C 11 (which I assume you have available as you're using nullptr
), you can use an alias template:
template <typename T>
using EditParamsCallback_t = WndDyn* (*)(T, const EditParams&);
int main() {
using Edit_t = EditParamsCallback_t<Point2d*>;
Edit_t g_Edit_CB{nullptr};
using Show_t = EditParamsCallback_t<const Point2d*>;
Show_t g_Show_CB{nullptr};
// Punkt3d
using Edit3d_t = EditParamsCallback_t<Point3d*>;
Edit3d_t g_Edit3d_CB{nullptr};
using Show3d_t = EditParamsCallback_t<const Point3d*>;
Show3d_t g_Show3d_CB{nullptr};
// Vector2d
}