由于定义表明其内存是固定的,下面程序中的静态变量如何存储新数字?

发布于 2025-01-09 03:50:55 字数 1451 浏览 0 评论 0原文

在我的 C++ 类的在线模块 zyBooks 中,静态数据成员定义为:

关键字 static 表示变量仅分配在内存中 在程序执行期间一次。静态变量驻留在 程序的静态内存区域并具有全局范围。因此,静态 可以从程序中的任何位置访问变量。

在类中,静态数据成员是该类的数据成员 每个类对象的数据成员。因此,静态数据成员是 独立于任何类对象,无需创建即可访问 一个类对象。

在下面的示例中,

class Store {
   public:
      Store(string storeName, string storeType);
      int getId();
      static int nextId;   // Declare static member variable  
   private:
      string name = "None";
      string type = "None";
      int id = 0;
};

Store::Store(string storeName, string storeType) {
   name = storeName;
   type = storeType;
   id = nextId;   // Assign object id with nextId
      
   ++nextId;      // Increment nextId for next object to be created
}
...
int Store::nextId = 101; // Define and initialize static data member

int main() {
   
   Store store1("Macy's", "Department");
   Store store2("Albertsons", "Grocery");
   Store store3("Ace", "Hardware");

   cout << "Store 1's ID: "<< store1.getId() << endl;
   cout << "Store 2's ID: "<< store2.getId() << endl;
   cout << "Store 3's ID: "<< store3.getId() << endl;
   cout << "Next ID: " << Store::nextId << endl;

   return 0;   
}
/*
OUTPUT:
Console
Store 1's ID: 101
Store 2's ID: 102
Store 3's ID: 103
Next ID: 104
*/

静态成员 nextId 如何存储创建的每个对象的递增数字?每次创建对象时都会占用内存中新的随机静态区域吗?或者静态数据成员的内存区域是否会扩展以容纳创建的每个对象的更多数字?

In the online module for my C++ class called zyBooks, static data members are defined as:

The keyword static indicates a variable is allocated in memory only
once during a program's execution. Static variables reside in the
program's static memory region and have a global scope. Thus, static
variables can be accessed from anywhere in a program.

In a class, a static data member is a data member of the class instead
of a data member of each class object. Thus, static data members are
independent of any class object, and can be accessed without creating
a class object.

In the example below,

class Store {
   public:
      Store(string storeName, string storeType);
      int getId();
      static int nextId;   // Declare static member variable  
   private:
      string name = "None";
      string type = "None";
      int id = 0;
};

Store::Store(string storeName, string storeType) {
   name = storeName;
   type = storeType;
   id = nextId;   // Assign object id with nextId
      
   ++nextId;      // Increment nextId for next object to be created
}
...
int Store::nextId = 101; // Define and initialize static data member

int main() {
   
   Store store1("Macy's", "Department");
   Store store2("Albertsons", "Grocery");
   Store store3("Ace", "Hardware");

   cout << "Store 1's ID: "<< store1.getId() << endl;
   cout << "Store 2's ID: "<< store2.getId() << endl;
   cout << "Store 3's ID: "<< store3.getId() << endl;
   cout << "Next ID: " << Store::nextId << endl;

   return 0;   
}
/*
OUTPUT:
Console
Store 1's ID: 101
Store 2's ID: 102
Store 3's ID: 103
Next ID: 104
*/

How does the static member nextId store the increasing numbers for each object created? Is a new, random static region in the memory occupied every time an object is created? Or Is the static data member's memory region expanding to hold more numbers for each object created?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋葬我深情 2025-01-16 03:50:55

来自 cpp 参考

静态数据成员不与任何对象关联。他们存在
即使没有定义该类的对象。只有一个
整个程序中静态数据成员的实例用static
存储时长,除非使用关键字thread_local,其中
如果每个线程有一个这样的对象,并且具有线程存储持续时间
(自 C++11 起)。

声明为静态的变量仅初始化一次,因为它们在单独的静态存储中分配空间,因此类中的静态变量由对象共享。不同对象不能有相同静态变量的多个副本。

From cpp reference:

Static data members are not associated with any object. They exist
even if no objects of the class have been defined. There is only one
instance of the static data member in the entire program with static
storage duration, unless the keyword thread_local is used, in which
case there is one such object per thread with thread storage duration
(since C++11).

The variables declared as static are initialized only once as they are allocated space in separate static storage so, the static variables in a class are shared by the objects. There can not be multiple copies of same static variables for different objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文