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

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

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

問題描述

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

【網(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)形?)
主站蜘蛛池模板: 精品久久久久久久人人人人传媒 | 夜夜骑首页 | 国产精品久久在线观看 | 综合九九 | 四虎成人精品永久免费av九九 | 国产视频1 | 国产一区二区三区视频在线观看 | 91在线看片 | 成人福利网 | 亚洲欧美精品国产一级在线 | 黄色av网站免费看 | 午夜免费福利影院 | 国产一区三区视频 | 日本在线一区二区 | 国产精品欧美一区二区三区不卡 | 久久久久久久久久久一区二区 | av免费入口| 欧美一级片免费看 | 国产亚洲欧美日韩精品一区二区三区 | 久久国产精品一区二区三区 | 99久久精品免费看国产四区 | 日本一道本 | 午夜视频一区 | 国产精品一区在线观看 | 日韩精品一区二区三区中文在线 | 99精品99| 国产视频欧美 | 国产亚洲成av人片在线观看桃 | 久久久久九九九女人毛片 | 狠狠伊人| 欧美一级黄色免费 | 日本不卡免费新一二三区 | 中文字幕日韩欧美 | 午夜91 | 亚洲欧洲综合av | 亚洲精品一区二区三区蜜桃久 | 亚洲免费在线 | 欧美成人一区二区三区片免费 | 日本午夜免费福利视频 | 欧美精品一区二区在线观看 | 亚洲精品无人区 |