简体   繁体   English

如何分配类中的静态成员?

[英]How is a static member in a class allocated?

If I have a class like 如果我有类似的课程

class MyClass {
   public:
      int myMember1;
      int myMember2;
      int myMember3;
};

Every time I instantiate an object of MyClass space for three consecutive int is allocated, what about when I have something like 每次我实例化一个MyClass空间的对象时,会分配三个连续的int ,那么当我有类似的东西时

class MyClass {
   public:
      static int myMember1;
      int myMember2;
      int myMember3;
};

How the memory is allocated this time? 这次如何分配内存?

I'm asking because I'm not entirely sure how the memory would be allocated when I declare multiple instance of the same class, is there a pointer to the static member maybe? 我问,因为当我声明同一个类的多个实例时,我不完全确定如何分配内存,是否有指向静态成员的指针?

As others already stated, you have to explicitly allocate space for the static member variable outside the class definition. 正如其他人已经说过的那样,您必须在类定义之外为静态成员变量显式分配空间。

In response to your other question, static member variables are not associated with class objects. 在回答您的其他问题时,静态成员变量与类对象无关。 That is to say, they will continue to exist even after your MyClass object has ceased to exist (until the termination of your program), and be shared across all instances of your class. 也就是说,即使您的MyClass对象不再存在(直到程序终止),它们仍将继续存在,并在您的类的所有实例之间共享。

Say you created multiple instances of the MyClass class like so: 假设您创建了MyClass类的多个实例,如下所示:

class MyClass {
public:
  static int myMember1;
  int myMember2;
  int myMember3;
};

int MyClass::myMember1 = 1;

int main()
{
   MyClass mc1;
   MyClass mc2;

   mc2.myMember1 = 2;

   std::cout << mc1.myMember1 << '\n';
   std::cout << mc2.myMember1 << '\n';
   return 0;
 }

The output will be: 输出将是:

2
2

How the memory is allocated this time [with the static member]? 这次[使用静态成员]如何分配内存?

Each instance of the object will have 2 integers in, and all instances have access to the static integer (but don't "own" it) - it is not part of the instances, it is in the scope of the class. 对象的每个实例都有2个整数,并且所有实例都可以访问static整数(但不“拥有”它) - 它不是实例的一部分,它在类的范围内。

NB the member is declared in the class, but must be defined outside the class (in the cpp file), eg; NB该成员在类中声明,但必须在类外部(在cpp文件中)定义,例如;

int MyClass::myMember1 = 42;

... is there a pointer to the static member maybe? ...是否有指向静态成员的指针?

No. You can get a pointer the static member if you require, but one is not allocated to each instance. 不可以。如果需要,可以将指针指向静态成员,但不会为每个实例分配一个指针。

The static member is allocated (and initialised as per the initialisation in the cpp file) when the application launches and can be accessed as other "global" objects are (albeit that the static is not in the global namespace, there is only one instance of it). 当应用程序启动时,静态成员被分配(并根据cpp文件中的初始化进行初始化),并且可以像其他“全局”对象一样进行访问(虽然静态不在全局命名空间中,但只有一个实例它)。 The accessibility of the member (ie public , private vs. protected ) follows the normal rules. 成员的可访问性(即publicprivateprotected )遵循正常规则。


To see the effect on the size, you can use sizeof() ; 要查看对大小的影响,可以使用sizeof() ;

class MyClass {
   public:
      int myMember1;
      int myMember2;
      int myMember3;
};

class MyClass1 {
   public:
      static int myMember1;
      int myMember2;
      int myMember3;
};

int MyClass1::myMember1 = 42;

int main(int argc, char* argv[])
{
    using namespace std;
    cout << sizeof(MyClass) << " " << sizeof(MyClass1) << endl;
}

The above (depending on alignment and the size of the int ), could produce an output of 12 8 . 上面(取决于对齐和int的大小),可以产生12 8的输出。

Demo 演示

You have to define the variable outside the class also. 您还必须在类外部定义变量。 That is where the actual allocation will be done. 这是实际分配的地方。

In essence it is the same as a global variable. 本质上它与全局变量相同。

class Test
{
    public:

    static int too; // Just a declaration
};

int Test::too; // Actual allocation, every instance will use this

You have to explicitly allocate memory for that static member somewhere. 您必须在某处为该静态成员显式分配内存。

For example, your class in in it's header file: 例如,您的类在其头文件中:

// myclass.h
class MyClass {
   public:
      static int myMember1;
      int myMember2;
      int myMember3;
};

And you have a cpp file for that class, which explicitly allocates space for that static member, it can also initialize it: 并且您有一个该类的cpp文件,它为该静态成员显式分配空间,它也可以初始化它:

// myclass.cpp
int MyClass::myMember1 = 5;

So your static member will be allocated at exactly one place in your program, in the translation unit of your choice. 因此,您的静态成员将在您选择的翻译单元中的程序中的一个位置进行分配。 You can place this allocation into any file you want, as long as it is only part of one translation unit. 您可以将此分配放入所需的任何文件中,只要它只是一个翻译单元的一部分即可。

Static membes are allocated in static memory 静态元素在静态内存中分配

You could think in these members as global variables, but declared inside the class scope (with corresponding access, in your case, public). 您可以将这些成员视为全局变量,但在类范围内声明(具有相应的访问权限,在您的情况下为public)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM