c++ - 代码中对象的大小

标签 c++

int Test::i; 行在下面的程序中做了什么。有人请解释

// Assume that integers take 4 bytes.
#include<iostream>

using namespace std;   

class Test
{
  static int i;
  int j;
};

int Test::i;

int main()
{
    cout << sizeof(Test);
    return 0;
}

最佳答案

这是定义类的 static 成员的语法。它将 Test::i 初始化为 0

要给它另一个值,你可以这样做

int Tent::i = 42;

关于c++ - 代码中对象的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13738346/

相关文章:

c++ - 如何在 C++ 中一点一点地循环 long long

c++ - 编译器使用的 double 表示的显式规范

C++ while循环从输入文件中读取

c++ - 链表,C++段错误

c++ - rxcpp 简单可观察<int>

c++ - 创建成员指向 int 或 short int 的结构数组?

c++ - 将 namespace 添加到具有 C header 的 C++ 实现

c++ - 在所有情况下将 keyPressEvent 传播到顶层 QWidget

c++ - 难以从 CImageList 设置菜单项位图

c++ - 为每个循环将迭代器分配给 std::vector 的当前元素