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

類聲明大括號后的分號

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

問題描述

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

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;

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

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.

我正在尋找更多與設(shè)計原理相關(guān)的東西,而不是能夠改變?nèi)魏螙|西,盡管一個好的代碼完成 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.

是的,人們確實會執(zhí)行您剛才在那里發(fā)表的聲明.它對于在方法內(nèi)部創(chuàng)建作用域類型很有用.

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

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

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.

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

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 天堂视频中文在线 | 91手机精品视频 | 在线免费观看日本视频 | 狠狠色狠狠色综合系列 | 日韩免费一二三区 | 五月天国产视频 | 国产一区 | 亚洲日韩中文字幕一区 | 91精品国产乱码久久久久久久久 | 日韩一区二区三区在线观看 | 精品无码久久久久久久动漫 | 国产高清在线精品 | 中文字幕亚洲一区 | 久久手机在线视频 | 日韩精品一区二区三区老鸭窝 | 亚洲精品在线91 | 91视频入口 | 黄色免费在线网址 | 日本亚洲精品成人欧美一区 | 在线观看黄色电影 | 欧美一区二区三区 | 97av视频在线观看 | 性生生活大片免费看视频 | 久久久久久久久久久久久9999 | 亚洲一区二区中文字幕 | 国产成人高清视频 | 日韩久久久久久 | 欧美一级在线观看 | 国产欧美在线播放 | 四虎影视在线 | 欧日韩不卡在线视频 | 久久精品国产99国产 | 日本精品一区 | 成人在线视频网址 | 久久久久久久久久影视 | 国产精品视频久久久 | 一区二区三区四区不卡视频 | 日韩中文视频 | 国产精品视频一区二区三区不卡 | 久草免费在线视频 | 亚洲视频网|