Static data members, static member function (Chapter 20)

Static member variables are variables shared by all instances of the same class, they are global data (available to all partially used) and member data (usually only for an object to use) compromise.

#include <iostream>

class Cat
{
public:
    Cat(int newAge = 1):age(newAge){ howManyCats++; }
    virtual ~Cat() { howManyCats--; }
    virtual int getAge() { return age; }
    virtual void setAge(int newAge) { age = newAge; }
    static int howManyCats;

private:
    int age;
};

int Cat::howManyCats = 0;

int main()
{
    const int maxCats = 5;
    Cat *catHouse[maxCats];
    int i;
    for (i = 0; i < maxCats; i++)
        catHouse[i] = new Cat(i);

    for (i = 0; i < maxCats; i++)
    {
        std::cout << "There are ";
        std::cout << Cat::howManyCats;
        std::cout << " cats left!\n";
        std::cout << "Deleting the one which is ";
        std::cout << catHouse[i]->getAge();
        std::cout << " years old\n";
        delete catHouse[i];
        catHouse[i] = 0;
    }
    return 0;
}

howmanycats variable is public, it can be accessed directly in the main. As long as the cat is always to access data through an example, it should be the other member variables and member functions declared private, and provide public accessor functions. On the other hand, if you want direct access to data without a cat object case, there are two options: he declared as public variable or a static member function.

Access to private static variables static member function

#include <iostream>

class Cat
{
public:
    Cat(int newAge = 1):age(newAge){ howManyCats++; }
    virtual ~Cat() { howManyCats--; }
    virtual int gGetAge() { return age; }
    virtual void setAge(int newAge) { age = newAge; }
    static int getHowMany() { return howManyCats; }
private:
    int age;
    static int howManyCats;
};

int Cat::howManyCats = 0;

void countCats();

int main()
{
    const int maxCats = 5;
    Cat *catHouse[maxCats];
    int i;
    for (i = 0; i < maxCats; i++)
    {
        catHouse[i] = new Cat(i);
        countCats();
    }

    for (i = 0; i < maxCats; i++)
    {
        delete catHouse[i];
        countCats();
    }
    return 0;
}

void countCats()
{
    std::cout << "There are " << Cat::getHowMany()
        << " cats alive!\n";
}

Since getHowMany () is public, any function can access it; at the same time, he was static, not by cat objects can also call it. Therefore, in line 41, the function countCats () to access the public static accessor functions, although he did cat object is available. Of course, also be the main () function, with the available object to call the cat getHowMany () function call other accessor functions as the same.

Note: this is not a static member function pointers . Therefore, they can not be declared as const. Further, since the member function in the member data is accessed by this pointer variable, so static member function can not access the non-static member variables.

Guess you like

Origin blog.csdn.net/suyunzzz/article/details/90081795