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

為什么不能在 C++ 中重新定義類中的類型名稱?

Why can#39;t redefine type names in class in C++?(為什么不能在 C++ 中重新定義類中的類型名稱?)
本文介紹了為什么不能在 C++ 中重新定義類中的類型名稱?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

根據本書 C++ Primer 部分,7.4.1 Type Names Are Special:

According to the book C++ Primer section, 7.4.1 Type Names Are Special:

通常,內部作用域可以從外部作用域重新定義名稱,即使該名稱已在內部作用域中使用.但是,在類中,如果成員使用外部作用域中的名稱并且該名稱是類型,則該類隨后可能不會重新定義該名稱.

Ordinarily, an inner scope can redefine a name from an outer scope even if that name has already been used in the inner scope. However, in a class, if a member uses a name from an outer scope and that name is a type, then the class may not subsequently redefine that name.

相應地,例如:

typedef double Money;
class Account {
    public:
        Money balance() { return bal; }
    private:
        typedef double Money;
        Money bal;
};

int main() {
    typedef double Money;
    Money asset;
    typedef double Money;
    return 0;
}

當你編譯上面的例子時,它會報錯:

When you compile the example above, it will complain:

a.cc:6:24: error: declaration of ‘typedef double Account::Money’ [-fpermissive]
         typedef double Money;
                        ^
a.cc:1:16: error: changes meaning of ‘Money’ from ‘typedef double Money’ [-fpermissive]
 typedef double Money;

那么為什么我們不能在類中重新定義類型名稱,而可以在內部作用域中?

So why can not we redefine type names in class, but can we in the inner scope?

我的編譯器版本是 g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609.
該部分還有一個注釋:

My compiler version is g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609.
And there is also a note in that section:

雖然重新定義類型名是錯誤的,但編譯器不需要診斷這個錯誤.一些編譯器會悄悄地接受這樣的代碼,即使程序出錯了.

Although it is an error to redefine a type name, compilers are not required to diagnose this error. Some compilers will quietly accept such code, even though the program is in error.

推薦答案

這不是類型獨有的.[basic.class.scope]/2:

This is not unique to types. [basic.class.scope]/2:

在類S中使用的名稱N應在其上下文以及在 S 的完整范圍內重新評估時.不違反此規則需要進行診斷.

A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

原因是類作用域中的名稱查找有點特殊.考慮:

The reason is that name lookup in class scope is a little special. Consider:

using Foo = int;

struct X {
    Foo a;    // ::Foo, i.e., int
    void meow() { 
        Foo b = a; // X::Foo; error: no conversion from int to char*
    }
    using Foo = char*;
};

成員函數體中的名稱查找考慮所有類成員,無論是在成員函數之前還是之后聲明(否則,在類定義中定義的成員函數將無法使用在類中稍后聲明的數據成員).結果是您得到兩個具有不同含義的 Foo ,即使它們在詞法上都在類成員 Foo 的聲明之前.這很容易導致非常混亂和脆弱的代碼,因此標準禁止它.

Name lookup in member function bodies considers all class members, whether declared before or after the member function (otherwise, a member function defined in a class definition wouldn't be able to use a data member declared later in the class). The result is that you get two Foos with different meanings, even though they both lexically precede the class member Foo's declaration. This can easily lead to extremely confusing and brittle code, and so the standard bans it.

這篇關于為什么不能在 C++ 中重新定義類中的類型名稱?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 国产高清视频一区 | 91av免费观看 | 性xxxxx| 成人激情视频免费观看 | 91精品一区 | 亚洲精品电影网在线观看 | 99精品欧美一区二区三区 | 日日骚网 | 三级黄色片在线观看 | 久久免费视频观看 | 日韩亚洲视频 | 欧美日韩国产一区二区三区 | 天天天操| 日本天堂视频在线观看 | 亚洲精品一区二区三区在线观看 | 中文字幕精品一区二区三区精品 | 欧美日韩激情 | 久久综合伊人 | 特级黄一级播放 | 欧美一区二区三区在线播放 | 国产精品99久久久久久动医院 | 欧美日韩综合 | 欧美精品一区二区三区蜜臀 | av中文字幕在线 | 一区二区三区影院 | 欧美日韩国产一区二区 | 精品1区| 国产精品成人一区二区三区夜夜夜 | 久久新视频 | 国产99久久 | 精品一区二区免费视频 | 欧美久久久 | 自拍中文字幕 | 精品久久国产老人久久综合 | 亚洲一区二区成人 | 欧美8一10sex性hd | 欧美在线一区二区三区 | 国产精品久久久久久福利一牛影视 | 久久成人一区 | 免费在线视频精品 | 欧美国产视频 |