【发布时间】:2021-08-18 17:40:57
【问题描述】:
我有一个有很多成员的结构。我决定为我的结构编写一些函数,但是将所有成员作为参数输入到函数调用中感觉不对。 有什么办法可以缩短吗?
items::items(std::string name_, std::string role_, uint16_t prize_, uint16_t phys_power_, uint16_t mag_power_,
uint16_t attack_speed_, uint16_t life_steal_, uint16_t flat_pen_, uint16_t perc_pen_, uint16_t crit_,
uint16_t phys_prot_, uint16_t mag_prot_, uint16_t cc_red_, uint16_t health_, uint16_t hp5_, uint16_t speed_,
uint16_t cd_red_, uint16_t mana_, uint16_t mp5_) {
name = name_;
role = role_;
prize = prize_;
phys_power = phys_power_;
mag_power = mag_power_;
attack_speed = attack_speed_;
life_steal = life_steal_;
flat_pen = flat_pen_;
perc_pen = perc_pen_;
crit = crit_;
phys_prot = phys_prot_;
mag_prot = mag_prot_;
cc_red = cc_red_;
health = health_;
hp5 = hp5_;
speed = speed_;
mana = mana_;
mp5 = mp5_;
}
void items::print_item(items s) {
std::cout << name << " " << role << " " << prize << " " << phys_power << " " << mag_power << " " << attack_speed << " " << life_steal << " " << flat_pen <<
" " << perc_pen << " " << crit << " " << phys_prot << " " << mag_prot << " " << cc_red << " " << health << " " << hp5 << " " << speed << " " << mana << " " <<
mp5 << std::endl;
}
【问题讨论】:
-
从使用初始化列表开始,移动复杂类型的语义和
print_items作为const成员函数。但除此之外,不,C++ 标准中没有任何内容。您可能想查看 ECS 库而不是这样做。 -
聚合初始化,(如果可能/适当的话)可能会缩短这个时间。
标签: c++ function struct repeat