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

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

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

問題描述

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

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

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

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.

相應(yīng)地,例如:

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;

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

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應(yīng)在其上下文以及在 S 的完整范圍內(nèi)重新評估時.不違反此規(guī)則需要進行診斷.

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*;
};

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

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.

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

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

相關(guān)文檔推薦

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++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
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 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 免费在线观看黄色片 | 天天燥日日燥 | 日本www在线观看 | 成人在线免费观看网站 | 欧美激情视频网站 | 欧美日韩一区二区三区视频 | 欧美成人猛片aaaaaaa | av不卡在线观看 | 伊人网在线视频 | 狠狠综合网 | 国产福利精品视频 | 欧美久久久久久久久 | 欧美日韩精品 | 福利小视频在线观看 | 中文字幕免费在线看线人动作大片 | 在线伊人 | 欧美九九九 | 羞羞网站在线观看 | 国产亚洲一区二区三区 | 午夜性影院 | 加勒比综合 | 99在线免费观看视频 | 黄片毛片 | 欧美在线日韩 | 黄色大片网站 | 日韩欧美在线播放 | 国产精品美女久久久久av爽 | 久久精品视频网站 | 亚洲久久久久久 | 91麻豆精品视频 | 日韩av一级片 | 国产精品一二三四区 | 真实的国产乱xxxx在线 | 日本一区二区在线播放 | 欧美性猛交xxxx黑人猛交 | 亚洲一区二区中文字幕 | 天天操天天碰 | 欧美综合在线视频 | 欧美日韩精品在线 | 亚洲色欧美 | 日韩a在线 |