使用嵌套的公共 C++ 类和枚举的优缺点是什么?例如,假设您有一个名为 printer 的类,并且该类还存储有关输出托盘的信息,您可以:
What are the pros and cons of using nested public C++ classes and enumerations? For example, suppose you have a class called printer, and this class also stores information on output trays, you could have:
class printer
{
public:
std::string name_;
enum TYPE
{
TYPE_LOCAL,
TYPE_NETWORK,
};
class output_tray
{
...
};
...
};
printer prn;
printer::TYPE type;
printer::output_tray tray;
或者:
class printer
{
public:
std::string name_;
...
};
enum PRINTER_TYPE
{
PRINTER_TYPE_LOCAL,
PRINTER_TYPE_NETWORK,
};
class output_tray
{
...
};
printer prn;
PRINTER_TYPE type;
output_tray tray;
我可以看到嵌套私有枚举/类的好处,但是当涉及到公共枚举/类时,办公室是分开的 - 这似乎更像是一种风格选择.
I can see the benefits of nesting private enums/classes, but when it comes to public ones, the office is split - it seems to be more of a style choice.
那么,你更喜欢哪个,为什么?
So, which do you prefer and why?
嵌套在类中的类有几个副作用,我通常认为这些是缺陷(如果不是纯粹的反模式).
There are several side effects to classes nested inside classes that I usually consider flaws (if not pure antipatterns).
让我们想象以下代码:
class A
{
public :
class B { /* etc. */ } ;
// etc.
} ;
甚至:
class A
{
public :
class B ;
// etc.
} ;
class A::B
{
public :
// etc.
} ;
所以:
作为结论,除非有异常(例如嵌套类是嵌套类的一个亲密部分......即使如此......),我认为在普通代码中嵌套类没有意义,因为缺陷的重要性超过了数量级感知到的优势.
As a conclusion, unless exceptions (e.g. the nested class is an intimate part of the nesting class... And even then...), I see no point in nested classes in normal code, as the flaws outweights by magnitudes the perceived advantages.
此外,它闻起来像是在不使用 C++ 命名空间的情况下模拟命名空间的笨拙尝试.
Furthermore, it smells as a clumsy attempt to simulate namespacing without using C++ namespaces.
在专业方面,您隔离此代码,如果是私有的,则使其无法使用但从外部"隔离类...
On the pro-side, you isolate this code, and if private, make it unusable but from the "outside" class...
优点:一切.
缺点:没什么.
事实是枚举项会污染全局范围:
The fact is enum items will pollute the global scope:
// collision
enum Value { empty = 7, undefined, defined } ;
enum Glass { empty = 42, half, full } ;
// empty is from Value or Glass?
通过将每个枚举放在不同的命名空间/类中可以避免这种冲突:
Ony by putting each enum in a different namespace/class will enable you to avoid this collision:
namespace Value { enum type { empty = 7, undefined, defined } ; }
namespace Glass { enum type { empty = 42, half, full } ; }
// Value::type e = Value::empty ;
// Glass::type f = Glass::empty ;
注意 C++0x 定义了类枚举:
Note that C++0x defined the class enum:
enum class Value { empty, undefined, defined } ;
enum class Glass { empty, half, full } ;
// Value e = Value::empty ;
// Glass f = Glass::empty ;
正是针对这类问题.
这篇关于使用嵌套 C++ 类和枚举的优缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 C++ 中读取和操作 CSV 文件数据?How can I read and manipulate CSV file data in C++?(如何在 C++ 中读取和操作 CSV 文件数据?)
在 C++ 中,为什么我不能像这样编写 for() 循环:In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,为什么我不能像这样编写 for() 循环: for(
OpenMP 如何处理嵌套循环?How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
在循环 C++ 中重用线程Reusing thread in loop c++(在循环 C++ 中重用线程)
需要精确的线程睡眠.最大 1ms 误差Precise thread sleep needed. Max 1ms error(需要精确的线程睡眠.最大 1ms 误差)
是否需要“do {...} while ()"?环形?Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?环形?)