久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用嵌套 C++ 類和枚舉的優(yōu)缺點?

Pros and cons of using nested C++ classes and enumerations?(使用嵌套 C++ 類和枚舉的優(yōu)缺點?)
本文介紹了使用嵌套 C++ 類和枚舉的優(yōu)缺點?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

使用嵌套的公共 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;

我可以看到嵌套私有枚舉/類的好處,但是當(dāng)涉及到公共枚舉/類時,辦公室是分開的 - 這似乎更像是一種風(fēng)格選擇.

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
  • 代碼冗長: 將類放入類只會使標(biāo)頭變大.您仍然可以將其分成多個聲明,但無法使用類似名稱空間的別名、導(dǎo)入或使用.
  • 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 亚洲国产精品成人无久久精品 | 亚洲视频二区 | 97久久精品午夜一区二区 | 国产99久久精品一区二区永久免费 | 日韩高清国产一区在线 | 欧美成人激情视频 | 国户精品久久久久久久久久久不卡 | 国产人成精品一区二区三 | 91亚洲国产成人精品一区二三 | 中文字幕视频在线观看 | 超碰成人免费 | 国产欧美一区二区三区在线看 | 亚洲国产免费 | 久久99精品久久久久久青青日本 | 精品国产乱码久久久久久1区2区 | 草久久久 | 亚洲精品大片 | 国产精品成人一区二区三区 | 韩国av一区二区 | 国产成人一区二 | 国产一区欧美 | 欧美日韩一区不卡 | 高清视频一区二区三区 | 久久麻豆精品 | 欧美一区不卡 | 欧美日韩亚洲一区 | 麻豆久久久9性大片 | 91在线网 | 久草在线 | 91久久久久久久久久久久久 | 国产日韩欧美在线观看 | 中文字幕乱码一区二区三区 | 国产高清一区 | 欧美中文字幕在线观看 | 亚洲精品女人久久久 | 日韩三级在线观看 | 亚洲网站在线观看 | 国产精品美女久久久久久久网站 | 中文字幕不卡视频在线观看 | 中文字幕一区二区三区四区不卡 | 欧美成人免费在线 |