C++静态成员变量详解

C++静态成员变量详解,第1张

概述默认情况下,每个类的对象(类的一个实例)都有它自己的类的成员变量副本。一个对象的成员变量与同一个类的其他对象的成员变量是独立分开并且有区别的。 例如,来看以下声明: 默认情况下,每个类的对象(类的一个实例)都有它自己的类的成员变量副本。一个对象的成员变量与同一个类的其他对象的成员变量是独立分开并且有区别的。

例如,来看以下声明:
class Widget{    private:        double price;        int quantity;    public:        Widget(double p,int q)        { price = p; quantity = q; }        double getPrice() const        { return price; }        int getQuantity() const { return quantity; }};
假定在某个程序中,使用以下声明创建了 Widget 类的两个单独的实例:

Widget w1(14.50,100),w2(12.75,500);

该语句创建了两个不同的对象 w1 和 w2。每个对象都有自己的 price 和 quantity 成员变量,如图 1 所示。

图 1 两个不同的对象 w1 和 w2
当任意一个实例的 getQuantity 成员函数被调用时,它将返回存储在调用对象的 quantity 变量中的值。基于最初存储在各自对象中的值,以下声明将显示 2 个不同的结果:

cout << w1.getQuantity () << " " << w2.getQuantity();

以上代码的显示结果为 "100 500"。

可以创建一个由同一个类的所有对象共享的成员变量。要创建这样的成员,只需将关键字 static 放在变量声明的前面,如下面的类所示:
class StatDemo{    private:        static int x;        int y;    public:        voID setx(int a) const { x = a; }        voID sety(int b) const { y = b; }        int getx() { return x; }        int gety() { return y; }};
接下来,在类之外放置一个单独的变量定义,例如:

int StatDemo::x;

在这个例子中,成员变量 x 将被 StatDemo 类的所有对象共享。当一个类对象将一个值放入 x 中时,它将出现在所有其他 StatDemo 对象中。

例如,假设程序中出现以下语句:
StatDemo obj1,obj 2;obj1.setx(5);obj1.sety(10);obj2.sety(20);cout << "x: " << obj1.getx () << " " << obj2.getx() << endl;cout << "y: " << obj1.gety () << " " << obj2.gety() << endl;
cout 语句将显示以下内容:

x: 5 5
y: 10 20

值 5 被对象 Obj1 存储在静态成员变量 X 中。由于 obj1 和 obj2 共享变量 X,所以值 5 显示在两个对象中,如图 2 所示。

图 2
下面的程序演示了一个更实用的静态成员变量用法。

Budget 类用于收集公司所有部门的预算请求。该类使用静态成员 corpBudget 来保存整个公司预算的金额。当成员函数 addBudget 被调用时,它的实参被添加到 corpBudget 的当前内容中。在程序完成时,corpBudget 将包含所有 Budget 类对象放置的所有值的总和。
//budget.h的内容#ifndef BUDGET_H#define BUDGET_Hclass Budget{    private:        static double corpBudget;        double divBudget;    public:        Budget() {divBudget =0;}        voID addBudget(double b)        {divBudget += b; corpBudget += divBudget; }        double getdivBudget() const { return divBudget; }        double getCorpBudget() const { return corpBudget; }};#endif//main程序的内容// This program demonstrates a static class member variable.#include <iostream>#include <iomanip>#include "budget.h"    // For Budget class declarationusing namespace std;// DeFinition of the static member of the Budget classdouble Budget::corpBudget = 0;int main(){    const int N_divISIONS = 4;    Budget divisions[N_divISIONS];    // Get the budget request for each division    for (int count = 0; count <N_divISIONS; count++)    {        double bud;        cout << "Enter the budget request for division ";        cout << (count + 1) << ": ";        cin >> bud;        divisions[count].addBudget(bud);    }    // display the budget request for each division    cout << setprecision(2);    cout << showpoint << fixed;    cout << "\nHere are the division budget requests: \n";    for (int count =0; count < N_divISIONS; count++)    {        cout << "division " << (count + 1) << "\t$ ";        cout << divisions[count].getdivBudget() << endl;    }    // display the total budget request    cout << "Total Budget Requests:\t$ ";    cout << divisions[0].getCorpBudget() << endl;    return 0;}
程序输出结果:

Enter the budget request for division 1:102000
Enter the budget request for division 2:202000
Enter the budget request for division 1:570000
Enter the budget request for division 1:100100
Here are the division budget requests:
division 1    $ 102000.00
division 2    $ 201000.00
division 3    $ 570000.00
division 4    $ 100100.00
Total Budget Requests:    $ 973100.00

注意,静态成员变量对于理解 C++ 声明和 C++ 定义之间的区别提供了一个很好的样板。声明提供了有关变量的存在和类型的信息;而定义则提供了声明中包含的所有信息,另外,还将为被定义的变量或函数分配内存。静态成员变量必须在类中声明并在类外部定义。

一般来说,可以将一个类的成员变量分成两组:实例成员和静态成员。实例成员是其使用必须与类的特定实例关联的成员。特别是,一个类的实例变量必须通过该类的特定实例来访问。

相比之下,静态成员变量的使用就不需要与任何实例关联。只有静态成员的类需要指定。 总结

以上是内存溢出为你收集整理的C++静态成员变量详解全部内容,希望文章能够帮你解决C++静态成员变量详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1231179.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存