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

類聲明大括號后的分號

Semicolon after class declaration braces(類聲明大括號后的分號)
本文介紹了類聲明大括號后的分號的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 C++ 類中,為什么右大括號后面有分號?我經常忘記它并得到編譯器錯誤,因此浪費了時間.對我來說似乎有些多余,事實并非如此.人們真的會做這樣的事情嗎:

In C++ classes, why the semi-colon after the closing brace? I regularly forget it and get compiler errors, and hence lost time. Seems somewhat superfluous to me, which is unlikely to be the case. Do people really do things like:

class MyClass
{
.
.
.
} MyInstance;

我是從結構和枚舉的 C 兼容性的角度來理解它的,但是由于類不是 C 語言的一部分,我猜它主要是為了保持類似聲明結構之間的一致性.

I get it from a C compatibility point of view for structs and enums, but since classes aren't part of the C language I guess it's primarily there the keep consistency between similar declaration constructs.

我正在尋找更多與設計原理相關的東西,而不是能夠改變任何東西,盡管一個好的代碼完成 IDE 可能會在編譯之前捕獲它.

What I was looking for was more related to design rationale rather than being able to change anything, although a good code completion IDE might trap this before compilation.

推薦答案

語言需要類型聲明中右大括號后的分號.從最早的 C 版本開始就是這樣.

The semi-colon after the closing brace in a type declaration is required by the language. It's been that way since the earliest versions of C.

是的,人們確實會執行您剛才在那里發表的聲明.它對于在方法內部創建作用域類型很有用.

And yes, people do indeed do the declaration you just put up there. It's useful for creating scoped types inside of methods.

void Example() {
  struct { int x; } s1;
  s1.x = 42;

  struct ADifferentType { int x; };
}

在這種情況下,我認為很清楚為什么需要分號.至于為什么在更一般的情況下需要在頭文件中聲明,我不確定.我的猜測是它是歷史性的,并且是為了讓編寫編譯器更容易.

In this case, I think it's clear why the semi-colons are needed. As to why it's needed in the more general case of declaring in the header file I'm unsure. My guess is that it's historical and was done to make writing the compiler easier.

這篇關于類聲明大括號后的分號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 午夜影晥 | 亚洲精品免费在线 | 99在线视频观看 | 亚洲一区二区三区四区在线观看 | 欧美成人一区二免费视频软件 | 国产精品国产a级 | 久久久综合久久 | 欧美视频免费在线 | 亚州av| 亚洲va国产日韩欧美精品色婷婷 | 人人人艹 | 狠狠操狠狠操 | 亚洲精品国产区 | 99久久精品视频免费 | 亚洲欧洲精品一区 | 亚洲第一天堂无码专区 | 亚洲二区精品 | 久久国产精品一区二区 | 久久在视频 | 可以免费看的毛片 | 亚洲第一av | 亚洲国产一区二区三区 | 精品国产一二三区 | 精品国产一区二区三区久久久蜜月 | www.亚洲区 | 久久国产精品久久久久久久久久 | 成人国产精品免费观看 | 欧美一区二区网站 | 久久影音先锋 | 久草日韩 | 欧美日日 | 久久久久国产 | 爱爱免费视频 | av一二三区 | 国产精品99久久久久久宅男 | 中文字幕在线视频免费视频 | 天天艹| 中文字幕在线视频网站 | 特黄毛片 | 在线播放一区二区三区 | 91在线 |