gpt4 book ai didi

c++ - 将相似类型的成员变量分组在一起

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:04:54 25 4
gpt4 key购买 nike

在编写类时,您会将相同类型的成员变量分组在一起吗?这样做有什么好处吗?例如:

class Foo
{
private:
bool a_;
bool b_;
int c_;
int d_;
std::string e_;
std::string f_;
...
};

相对于:

class Bar
{
private:
std::string e_;
bool a_;
int d_;
bool b_;
std::string f_;
int c_;
.

..};

或者您只是按照添加的顺序排列它们?

最佳答案

我根据语义对它们进行分组,即

class Foo
{
private:
std::string peach;
bool banana;
int apple;

int red;
std::string green;
std::string blue;
...
};

可读性越高越好。

关于c++ - 将相似类型的成员变量分组在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/325734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com