我的类Menu中有一个私有结构,其中包含菜单项属性。这些属性之一是指向函数的指针。当我在类外转发声明函数时,似乎我写的东西行得通,但是在类外定义函数似乎是非常糟糕的做法。
#pragma once
#include <string>
//forward declaration outside the class
void completelyRandom();//does not throw error
class Menu
{
private:
typedef void(*Menu_Processing_Function_Ptr)();
struct MenuItem
{
unsigned int number;
const char * text;
Menu_Processing_Function_Ptr p_processing_function;
};
MenuItem menu[] =
{
{1,"Completely random",completelyRandom}
};
public:
Menu();
~Menu();
};
这是一些引发错误但更接近我希望的代码:
#pragma once
#include <string>
class Menu
{
private:
typedef void(*Menu_Processing_Function_Ptr)();
struct MenuItem
{
unsigned int number;
const char * text;
Menu_Processing_Function_Ptr p_processing_function;
};
MenuItem menu[1] =
{
{1,completelyRandom}
};
public:
Menu();
//declaration within the class
void completelyRandom();//does throw error
~Menu();
};
我尝试在Menu范围内到处移动completelyRadom()声明,但出现错误
a value of type "void (Menu::*)()" cannot be used to initialize an entity of type "Menu::Menu_Processing_Function_Ptr"
有没有一种方法可以向前声明最佳实践中的功能?还是我应该为我糟糕的设计选择感到遗憾并重新开始?