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

C++ 中帶有 int 、函數(shù)、虛函數(shù)的 sizeof 類?

sizeof class with int , function, virtual function in C++?(C++ 中帶有 int 、函數(shù)、虛函數(shù)的 sizeof 類?)
本文介紹了C++ 中帶有 int 、函數(shù)、虛函數(shù)的 sizeof 類?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

這是一道在線C++試題,已經(jīng)做完了.

This is an online C++ test question, which has been done.

#include<iostream>
using namespace std; 
class A
{

};
class B
{
int i; 
}; 

class C
{
void foo();
};
class D
{
virtual void foo();
};

class E
{
int i ; 
    virtual void foo();
};
class F
{
int i; 
    void foo();
};
class G
{
    void foo();
    int i;
    void foo1();
};

class H
{
    int i ;
    virtual void foo();
    virtual void foo1();
};
int main()
{
cout <<"sizeof(class A) : " << sizeof(A) << endl ;
cout <<"sizeof(class B) adding the member int i : " << sizeof(B) << endl ;
cout <<"sizeof(class C) adding the member void foo() : " << sizeof(C) << endl ;
cout <<"sizeof(class D) after making foo virtual : " << sizeof(D) << endl ;
cout <<"sizeof(class E) after adding foo virtual , int : " << sizeof(E) << endl ;
cout <<"sizeof(class F) after adding foo  , int : " << sizeof(F) << endl ;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(G) << endl ;
G g;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(g) << endl ;
cout <<"sizeof(class H) after adding int 2 virtual " << sizeof(H) << endl ;
return 0; 
}

輸出:

sizeof(class A) : 1
sizeof(class B) adding the member int i : 4
sizeof(class C) adding the member void foo() : 1
sizeof(class D) after making foo virtual : 8
sizeof(class E) after adding foo virtual , int : 16
sizeof(class F) after adding foo  , int : 4
sizeof(class G) after adding foo   , unsigned int : 4
sizeof(class g) after adding foo  , unsigned int : 4
sizeof(class H) after adding int 2 virtual 16

我的問題:

為什么 siszeof(A) 是 1 而 sizeof(C) 也是 1?

Why siszeof(A) is 1 and sizeof(C) is 1 too ?

為什么 siszeof(H) 是 16 而 sizeof(G) 是 4 ?

Why siszeof(H) is 16 but sizeof(G) is 4 ?

為什么 siszeof(E) 是 16 而 sizeof(F) 是 4 ?

Why siszeof(E) is 16 but sizeof(F) is 4 ?

為什么 siszeof(D) 是 8 而 sizeof(E) 是 16 ?

Why siszeof(D) is 8 but sizeof(E) is 16 ?

我的猜測:

虛函數(shù)是一個8字節(jié)的指針.但是,我不知道為什么 E 大小是 16 ?向空類添加函數(shù)不會改變其大小?

A virtual function is a pointer with 8 bytes. But, I do not know why E size is 16 ? Adding a function to an empty class does not change its size ?

感謝任何幫助.

謝謝

推薦答案

首先,虛函數(shù)不是一個 8 字節(jié)的指針.在 C++ 中,只有 sizeof(char) 保證是任意數(shù)量的字節(jié).

First off, a virtual function is not a pointer with 8 bytes. In C++ nothing but sizeof(char) is guaranteed to be any number of bytes.

第二,只有類中的第一個虛函數(shù)會增加其大小(依賴于編譯器,但在大多數(shù)情況下 - 如果不是全部 - 就像這樣).所有后續(xù)方法都沒有.非虛函數(shù)不影響類的大小.

Second, only the first virtual function in a class increases its size (compiler-dependent, but on most - if not all - it's like this). All subsequent methods do not. Non-virtual functions do not affect the class's size.

發(fā)生這種情況是因為類實例不保存指向方法本身的指針,而是指向虛擬函數(shù)表,每個類一個.

This happens because a class instance doesn't hold pointers to methods themselves, but to a virtual function table, which is one per class.

如果你有:

class A
{
   virtual void foo();
}

class B
{
   virtual void goo();
   virtual void test();
   static void m();
   void x();
}

你會有 sizeof(A) == sizeof(B).

現(xiàn)在:

為什么 siszeof(A) 是 1 而 sizeof(C) 也是 1?

Why siszeof(A) is 1 and sizeof(C) is 1 too ?

AC 的大小為 1 只是因為不允許類的大小為 0.函數(shù)與它無關(guān).這只是一個虛擬字節(jié).

A and C have size 1 just because it's not allowed for a class to be of size 0. The functions have nothing to do with it. It's just a dummy byte.

為什么 siszeof(H) 是 16 而 sizeof(G) 是 4 ?

Why siszeof(H) is 16 but sizeof(G) is 4 ?

G 只有一個成員占內(nèi)存 - int.在你的平臺上,sizeof(int) == 4.H,除了int,還有一個指向vftable(虛函數(shù)表,見上)的指針.this 的大小、int 的大小和對齊方式是特定于編譯器的.

G has only one member that accounts for memory - the int. And on your platform, sizeof(int) == 4. H, besides the int, also has a pointer to the vftable (virtual function table, see above). The size of this, size of int and allignment are compiler specific.

為什么 siszeof(E) 是 16 而 sizeof(F) 是 4 ?

Why siszeof(E) is 16 but sizeof(F) is 4 ?

上面解釋了 - 非虛方法不占用類中的內(nèi)存.

Explained above - non virtual methods don't take up memory in the class.

為什么 siszeof(D) 是 8 而 sizeof(E) 是 16 ?

Why siszeof(D) is 8 but sizeof(E) is 16 ?

D 僅包含 vftable 指針,它在您的平臺上顯然是 8 個字節(jié).E 也有一個 int,vftable 對齊到 8 個字節(jié).所以它是這樣的:

D only contains the vftable pointer which is apparently 8 bytes on your platform. E also has an int, and the vftable is aligned to 8 bytes. So it's something like:

class E

4 bytes for int |  4 padding bytes  |  8 bytes for vftable pointer  | 
| x | x | x | x |    |    |    |    | v | v | v | v | v | v | v | v |

這篇關(guān)于C++ 中帶有 int 、函數(shù)、虛函數(shù)的 sizeof 類?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)形?)
主站蜘蛛池模板: 亚洲在线一区二区三区 | 亚洲人成在线观看 | 成人精品免费视频 | 欧美美女二区 | 国产福利在线视频 | 成人在线一区二区 | 国产激情偷乱视频一区二区三区 | 久久国产成人午夜av影院武则天 | 岛国午夜 | 在线观看黄视频 | 成人黄色电影在线播放 | 久久中文字幕一区 | 黄篇网址 | 91小视频在线| 天天天操天天天干 | 日韩一级黄色毛片 | 久久久久久国产精品 | 久久国产精品视频 | 99久久久久国产精品免费 | 亚洲精品国产a久久久久久 午夜影院网站 | 久久久久久国产一区二区三区 | 欧美日韩亚洲在线 | 国产精品一区二区免费 | 国产成人99久久亚洲综合精品 | av看看| 久久99视频免费观看 | 成年人视频在线免费观看 | 日韩精品一区二区三区在线 | 精品国模一区二区三区欧美 | 亚洲每日更新 | 久久久久久久久99 | 日韩电影中文字幕在线观看 | 日韩在线| 乱一性一乱一交一视频a∨ 色爱av | 亚洲一一在线 | 免费性视频 | 色精品 | 宅男噜噜噜66一区二区 | 亚洲精品视频播放 | www.久草.com | 91精品国产综合久久久久久 |