問題描述
使用嵌套的公共 C++ 類和枚舉的優(yōu)缺點是什么?例如,假設(shè)您有一個名為 printer
的類,并且該類還存儲有關(guān)輸出托盤的信息,您可以:
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?
推薦答案
嵌套類
嵌套在類中的類有幾個副作用,我通常認為這些是缺陷(如果不是純粹的反模式).
Nested classes
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.
} ;
所以:
- 特權(quán)訪問: A::B 對 A 的所有成員(方法、變量、符號等)具有特權(quán)訪問權(quán)限,這削弱了封裝性
- A 的范圍是符號查找的候選: 來自 B 內(nèi)部的代碼將看到來自 A 的 所有 符號作為符號查找的可能候選,這可能會混淆代碼莉>
- forward-declaration: 沒有完整的 A 聲明就無法前向聲明 A::B
- 可擴展性:除非您是 A 的所有者,否則不可能添加另一個類 A::C
- 代碼冗長: 將類放入類只會使標頭變大.您仍然可以將其分成多個聲明,但無法使用類似名稱空間的別名、導入或使用.
- Privilegied Access: A::B has privilegied access to all members of A (methods, variables, symbols, etc.), which weakens encapsulation
- A's scope is candidate for symbol lookup: code from inside B will see all symbols from A as possible candidates for a symbol lookup, which can confuse the code
- forward-declaration: There is no way to forward-declare A::B without giving a full declaration of A
- Extensibility: It is impossible to add another class A::C unless you are owner of A
- Code verbosity: putting classes into classes only makes headers larger. You can still separate this into multiple declarations, but there's no way to use namespace-like aliases, imports or usings.
作為結(jié)論,除非有異常(例如嵌套類是嵌套類的一個親密部分......即使如此......),我認為在普通代碼中嵌套類沒有意義,因為缺陷的重要性超過了數(shù)量級感知到的優(yōu)勢.
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.
在專業(yè)方面,您隔離此代碼,如果是私有的,則使其無法使用但從外部"隔離類...
On the pro-side, you isolate this code, and if private, make it unusable but from the "outside" class...
優(yōu)點:一切.
缺點:沒什么.
事實是枚舉項會污染全局范圍:
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 ;
正是針對這類問題.
這篇關(guān)于使用嵌套 C++ 類和枚舉的優(yōu)缺點?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!