久久久久久久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 轉換單一顏色)
主站蜘蛛池模板: 日韩在线播放第一页 | 日韩电影中文字幕 | 一级黄色网页 | 国产精品区二区三区日本 | 91精品国产777在线观看 | 99资源站 | 亚洲女人天堂成人av在线 | 色吧综合| 亚洲日本一区二区 | 亚洲色图综合 | 精品日韩一区 | 午夜小影院 | 麻豆亚洲 | 久久婷婷色 | 黄色在线观看国产 | 在线观看国产视频 | 亚洲国产精品一区二区三区 | 国产精品1区 | 国产精品毛片一区二区三区 | 精品欧美一区二区三区久久久小说 | 蜜桃视频成人 | 美女一级毛片 | 91精品国产91久久久久久不卞 | 国产91综合一区在线观看 | 欧美亚洲国产一区二区三区 | www.日韩欧美 | a级毛片免费高清视频 | 午夜视频免费在线观看 | 激情婷婷| 亚洲国产高清在线观看 | 免费黄色a级毛片 | 国产精品久久久久久中文字 | 成人国产在线视频 | 性高湖久久久久久久久3小时 | 欧美日韩一 | 日韩国产中文字幕 | 亚洲精品一二三区 | 在线观看国产h | 国产一区二区三区四区 | 国产日韩精品一区二区 | www亚洲精品 |