【发布时间】:2021-03-12 11:51:57
【问题描述】:
我正在尝试在C++ 中创建一个命令行游戏。我是新手,刚开始学习。
我的游戏由玩家类和龙类组成。这些类在单独的头文件中声明。
我想知道的是,是否有办法在声明后调用类的随机函数。
喜欢
// ignore the includes
class foo{
public:
string name = "foo";
foo(string name){
this->name = name;
}
void func1(){
//some code
}
void func2(){
//some code
}
void func3(){
//some code
}
}
/////
//main.cpp
#include <iostream>
#include "foo.h"
int main(){
foo bar("hello");
//call a random function like func1, func2, func3;
return 0;
}
【问题讨论】:
-
你可以使用 switch(randomNumber) { case 1: func1();休息; ...或使用函数指针数组。
标签: c++ function random header