C + + Object count

Source: Internet
Author: User

Object count is a common technique in C + +, and many methods of object counting are encapsulated in the-X engine, which summarizes the common object count methods after reviewing the relevant data.

A relatively simple single-class count is:

class Sprite{public:    Sprite() { m_count++; };        ~Sprite() { m_count--; };    int getCount() { return m_count; };private:    static int m_count;};int Sprite::m_count = 0;

The static type of the member variable guarantees the sharing of all objects, if the sprite is derived from a base class, the base class destructor must be virtual function, for many reasons, the previous article also said that the destructor of the base class is a virtual function can be guaranteed if the base class pointer to the derived class object, However, when you delete an object with a pointer to a base class, you ensure that no resource leaks occur. The reason for this is to ensure that the object count is normal.

class Node{};class Sprite : public Node...Node* ptr = new Sprite;delete ptr;

At this point, if node's destructor is not a virtual function, delete calls the node's destructor, and the sprite's count value will be problematic. Of course, from the idea of encapsulation, if the operation of each class is certainly not possible, so we can use the template (template class packaging to ensure that the different class count of independent) to unify the encapsulation.

template <class T>class ObjectCounter{public:    ObjectCounter() { m_count++; };    ~ObjectCounter() { m_count--; };    int getObjectCounter() { return m_count; };private:    static int m_count;};template <class T>int ObjectCounter<T>::m_count = 0;class Sprite : public Node{public:private:    ObjectCounter<Sprite> m_counter;};

Here's an application, assuming I'm a blizzard programmer to represent the night elf units for the Warcraft design program, this time we try to follow the above method.

class NightElfUnit{};class Archers : public NightElfUnit{    ObjectCounter<Archers> m_counter;};class Huntress : public NightElfUnit{    ObjectCounter<Huntress> m_counter;};class Deer : public NightElfUnit{    ObjectCounter<Deer> m_counter;};

This time we may have seen their own mistakes, the population of Warcraft is shared, and our count is shared, if added to the base class, Warcraft different classes occupy the population is different, this time should do? We can modify our counter class.

template <class T>class ObjectCounter{public:    ObjectCounter(int step = 1)     {        m_step = step;        m_count += m_step;    };    ~ObjectCounter()    {        m_count -= m_step;    };    int getObjectCounter() { return m_count; };private:    static int m_count;    int m_step;};

This allows member variables to be initialized to the Objectcounter type in units such as archers,huntress, sharing a unified counter, and specifying a population when initializing.

Of course, C + + object Count method A lot, take time to catch the-x3.0 code, look at the internal counting method implementation.

C + + Object count

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.