本文介紹了為什么一個類可以有自己的靜態成員,而不是非靜態成員?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
class base {
public:
base a;
};
它會導致編譯錯誤.
class base {
public:
static base a;
};
而此代碼不會給出編譯錯誤
whereas this code does not give compilation error
推薦答案
因為 static
類成員沒有存儲在類實例中,這就是 static
可以工作的原因.
Because static
class members are not stored in the class instance, that's why a static
would work.
將一個對象存儲在另一個相同類型的對象中會破壞運行時 - 無限大,對吧?
Storing an object inside another object of the same type would break the runtime - infinite size, right?
sizeof
會返回什么?編譯器需要知道對象的大小,但是因為它包含了同類??型的對象,所以沒有意義.
What would sizeof
return? The size of the object needs to be known by the compiler, but since it contains an object of the same type, it doesn't make sense.
這篇關于為什么一個類可以有自己的靜態成員,而不是非靜態成員?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!