由于定义表明其内存是固定的,下面程序中的静态变量如何存储新数字?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 cpp 参考:
声明为静态的变量仅初始化一次,因为它们在单独的静态存储中分配空间,因此类中的静态变量由对象共享。不同对象不能有相同静态变量的多个副本。
From cpp reference:
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.