C++ 如何在类中显示函数
对于我正在处理的项目,我想在命令提示符下向用户显示所有可用的函数,现在除了键入每个函数外,是否还有显示这些函数的方法 例如:C++ 如何在类中显示函数,c++,C++,对于我正在处理的项目,我想在命令提示符下向用户显示所有可用的函数,现在除了键入每个函数外,是否还有显示这些函数的方法 例如: Functions I want displayed bool status(); double getTime(); void setTime(int h, int min, int sec); double getDate(); void setDate(int d,int m,int y); void setAuto(); void turnOn(); void
Functions I want displayed
bool status();
double getTime();
void setTime(int h, int min, int sec);
double getDate();
void setDate(int d,int m,int y);
void setAuto();
void turnOn();
void turnOff();
这些是在一个我称为Audio_Visual.h的类中声明的
//Have a small welcome text
cout << "Welcome to this Home Automation System" << endl;
cout << "Here Are a list of functions, please type what you want to do" << endl;
// DISPLAY FUNCTIONS
//display current function such as lights.setAuto();
string input = "";
//How to get a string/sentence with spaces
cout << "Please enter a valid function" << endl;
getline(cin, input);
cout << "You entered: " << input << endl << endl;
<>我是如何编写命令行的东西这是不容易做到的,因为C++没有实现。
例如,其他语言Java和C中也存在这样的结构。我建议您制作一个表。这通常称为查找表 搜索函数名,然后在同一位置取消引用函数指针 缺点是所有函数必须具有相同的签名
另一种选择是使用Factory设计模式。您可以使用映射将函数名映射到函数指针
typedef void ( Audio_Visual::* fv)();
std::map<std::string, fv> void_functions;
或
对于不同的签名,您需要不同的映射:即
typedef double ( Audio_Visual::* fd)();
std::map<std::string, fd> double_functions;
或
用法:
Audio_Visual a;
void_functions[ "turnOn"] = &Audio_Visual::turnOn;
std::cout << "Now we are turning on...";
(a.(*void_functions[ "turnOn"]))();
在这里,您可以找到关于第二个选项的更多信息:C++不支持反射,因此无法迭代类的成员函数 但是,我建议您将设计分为两部分:操作和实现本身 不同的操作做不同的事情,即调用实现端的不同功能。因此,一个动作是采取必要的用户输入并调用适当的实现函数的东西。一些简单的例子:
void get_status_action()
{
//Just propmts the status:
std::cout << impl.status();
}
void set_time_action()
{
int hour , minute , second;
//Get user input:
std::cout << "Please enter the time: ";
std::cin >> hour >> minute >> second;
//Call the implementation:
impl.steTime( hour , minute , second );
}
现在,您想要的是以用户可以轻松选择的方式存储操作。使用从操作名称到操作的映射字符串。注意,在我的示例中,操作只是使用全局实现实例的函数。您可以编写一些复杂的操作实现,如函子、类等
他可以使用一些奇怪的C宏,比如操作符来字符串化函数名。但是当然,没有办法迭代将其字符串化的成员函数。我会认真考虑构建一个JNI。那么,是否只使用查找表来显示类中的函数呢?或者可以用来检查用户是否输入了有效的函数。您必须手动将函数名输入表中。是的,如果用户的文本不在表中,则函数名无效。您可以尝试分析标头。但你没有具体说明,你想要它做什么?
Audio_Visual a;
void_functions[ "turnOn"] = &Audio_Visual::turnOn;
std::cout << "Now we are turning on...";
(a.(*void_functions[ "turnOn"]))();
void get_status_action()
{
//Just propmts the status:
std::cout << impl.status();
}
void set_time_action()
{
int hour , minute , second;
//Get user input:
std::cout << "Please enter the time: ";
std::cin >> hour >> minute >> second;
//Call the implementation:
impl.steTime( hour , minute , second );
}
int main()
{
std::unordered_map<std::string,std::function<void()>> actions;
actions["Set status"] = set_status_action;
actions["Set current time"] = set_time_action;
while( true )
{
//Propmt the actions:
std::cout << "Select one of the following actions:" << std::endl;
for( auto& pair : actions )
std::cout << pair.first << std::endl;
//Read the selection and execute the proper action:
std::string selection;
std::getline( std::cin , selection );
actions[selection]();
}
}