需要帮助改进我的 c++ 和类创建
Need help improving my c++ and class creation
我几乎是一个初学者/自学C++程序员。 我想测试一下自己的技能。 我做了这个程序,如果你的男孩/女孩能告诉我如何改进它,我会很高兴。这是我的第一堂课(请告诉我,如果你想清理一下)
class race {public:
enum raceOpt { orc, elf, human };
race(raceOpt c_raceOpt, int *STR, int *DEX, int *CON, int *INT, int* WIS, int* CHR);
//race opt outcomes
void ORC(int** STR, int** INT, int** CHR) {
**STR = **STR + 2;
**INT = **INT - 2;
**CHR = **CHR - 2;
}
void ELF(int** DEX) {
//elf code and stat changes
}
//I still have to add human
};
// here's my constructor
race::race(raceOpt c_raceOpt, int* STR, int* DEX, int* CON, int* INT, int* WIS, int* CHR) {
switch (c_raceOpt) {
case orc:
ORC(&STR, &INT, &CHR);
break;
case human:
//human code
break;
case elf:
//elf code
break;
}
}
这是我的下一个基类
class classes {
public:
enum classOpt { fighter, mage, soccerPlayer };
classes(classOpt c_classOpt, double ModStats[_MAX_PATH], int* Sneak, int* Bluff, int* Deplomacy, int* Swim, int* Mana, int* Run);
void Fighter(int** SWIM, int** RUN, int** BLUFF, double ModStats[_MAX_PATH]) {
**SWIM = static_cast<int>(**SWIM + std::round(((ModStats[2] * 2.3))));
**RUN = static_cast<int>(**RUN + (std::round(((ModStats[2] * 2)))));
**BLUFF = static_cast<int>(**BLUFF + std::round(((ModStats[1] * 2.5))));
}
void Mage(int** MANA, int** DEPLOMACY, int** BLUFF, double ModStats[_MAX_PATH]) {
//code to be added
}
void SoccerPlayer(int** RUN, int** SWIM, int** DEPLOMACY, int** SNEAK, double ModStats[_MAX_PATH]) {
//code to be added
}
};
// here's my constructor
classes::classes(classOpt c_classOpt, double ModStats[_MAX_PATH], int* Sneak, int* Bluff, int* Deplomacy, int* Swim, int* Mana, int* Run) {
switch (c_classOpt) {
case fighter:
Fighter(&Swim, &Run, &Bluff, ModStats);
break;
case mage:
Mage(&Mana, &Deplomacy, &Bluff, ModStats);
break;
case soccerPlayer:
SoccerPlayer(&Run, &Swim, &Deplomacy, &Sneak, ModStats);
break;
}
}
这是我的最后一个英雄类构造函数和主要。
class hero {
protected:
int sneak = 0, bluff = 0, deplomacy = 0, swim = 0, mana = 0, run = 0;
int //stats
Str = 16,
Dex = 18,
Con = 11,
Int = 10,
Wis = 8,
Chr = 13;
double modStats[6] = {};
//vvvv gets proper stat mods based on D&D
void getAndSetMods(){
double modStats[6] = {
this->modStats[1] = std::round(Str / 2) - 5,
this->modStats[2] = std::round(Dex / 2) - 5,
this->modStats[3] = std::round(Con / 2) - 5,
this->modStats[4] = std::round(Int / 2) - 5,
this->modStats[5] = std::round(Wis / 2) - 5,
this->modStats[6] = std::round(Chr / 2) - 5,
};//stats above in order
}
public:
void raceAndClass() {
race c_race(race::orc, &Str, &Dex, &Con, &Int, &Wis, &Chr);
getAndSetMods();
classes c_classes(classes::fighter, modStats, &sneak, &bluff, &deplomacy, &swim, &mana, &run);
using
namespace std;
cout << Str << endl << Dex << endl << Con << endl << Int << endl << Wis << endl << Chr << endl;
cout << endl << endl << endl;
cout << sneak << endl << bluff << endl << deplomacy << endl << swim << endl << mana << endl << run << endl;
}
};
int main()
{
hero H;
H.raceAndClass();
while (true);
}
第一个指针,你的指针太多了。
在任何地方获取变量的地址,因为您需要在被调用的方法中更改它,因此您应该在被调用的方法中使用引用。
SomeCall(&someVar);
void SomeCall(int* theVar);
应该是
SomeCall(someVar);
void SomeCall(int& theVar);
创建帮助程序类统计信息
class Stats {
protected: // or private and write a bunch of access methods
int //default stats
Str = 16,
Dex = 18,
Con = 11,
Int = 10,
Wis = 8,
Chr = 13;
Stats(int STR, int DEX, int CON, int INT, int WIS, int CHR) :
Str(STR), Dex(DEX), Con(CON), Int(INT), Wis(WIS), Chr(CHR) {
}
}
那么你的比赛类可以这样简化
class race {
public:
enum raceOpt { orc, elf, human };
race(raceOpt c_raceOpt, Stats& stats) {
switch (c_raceOpt) {
case orc:
ORC(stats);
//race opt outcomes
void ORC(Stats& stats) {
stats.Str += 2;
stats.Int -= 2;
stats.Chr -= 2;
}
重构可以继续
- 使用 std::array 来保存 Stats 类中的统计信息。
- 将 modStat 移动到 Stats 类,因为它们是相同的问题。 潜在
- 为统计信息创建一个枚举以使用查找(不是枚举类,因为这在这里会很痛苦)。
- 为每个主要概念创建一个类。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 终端不会为C++文件创建.exe文件吗
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息