久久久久久久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 轉換單一顏色)
主站蜘蛛池模板: 欧美国产视频一区二区 | 欧美日韩亚洲三区 | 亚洲视频一区 | 黄网免费 | 一本一道久久a久久精品综合蜜臀 | 日韩欧美二区 | 99re在线观看 | 亚洲精品久久久久久一区二区 | 国产免费又黄又爽又刺激蜜月al | 国内精品伊人久久久久网站 | 免费xxxx大片国产在线 | 呦呦在线视频 | 中文字幕在线免费观看 | 男女视频在线观看免费 | 亚洲视频精品 | 成人在线观看网站 | 欧美综合国产精品久久丁香 | 日本不卡一区 | 精品欧美黑人一区二区三区 | 高清一区二区 | 欧美日韩一区二区三区视频 | 日韩手机视频 | 中文字幕精品一区二区三区精品 | 夜夜操天天操 | 成人在线观看免费 | 人人九九精 | 免费视频99 | 国产中文| 欧美三级免费观看 | 青青久久av北条麻妃海外网 | 午夜无码国产理论在线 | 久久亚洲一区二区三区四区 | 国产精品久久久久久网站 | 青娱乐一区二区 | 欧美视频精品 | 欧美电影免费观看高清 | 欧美日韩免费 | 乱码av午夜噜噜噜噜动漫 | 亚洲欧洲综合av | 色免费视频 | 波多野结衣精品在线 |