简体   繁体   English

计数器对象作为类数据成员

[英]Counter object as a class data member

I am reading Counting Objects in C++ from Scott Meyers: http://www.drdobbs.com/cpp/counting-objects-in-c/184403484 . 我正在阅读Scott Meyers的C ++中的Counting Objects: http//www.drdobbs.com/cpp/counting-objects-in-c/184403484 He defines a Counter class: 他定义了一个Counter类:

class Counter {  
public:          
    Counter() { ++count; }
    Counter(const Counter&) { ++count; }
    ~Counter() { --count; }
    static size_t howMany()
        { return count; }

private:
    static size_t count;
};
// This still goes in an
// implementation file
size_t Counter::count = 0;

The article says that one way to use this class is as a class data member: 文章说使用这个类的一种方法是作为类数据成员:

class Widget {
public:
    .....  // all the usual public
           // Widget stuff
    static size_t howMany()
    { return Counter::howMany(); }
private:
    .....  // all the usual private
           // Widget stuff
    Counter c;
};

My question is related to the expression Counter c; 我的问题与Counter c;的表达有关Counter c; . If we are using a static method of Counter , why is a Counter object inside Widget declared? 如果我们使用Counter的静态方法,为什么声明WidgetCounter对象?

It is there in order to increment the counter when a new instance of Widget is created. 它是为了在创建Widget的新实例时递增计数器。

When a Counter is a member of Widget , the constructor of Counter gets invoked whenever a Widget is created. Counter为成员Widget的构造Counter ,每当一个被调用Widget创建。 This in turn, increments the value of the count variable of the Counter . 这又会增加Countercount变量的值。

The reason the Counter object works is that it does its job in its constructors and destructors. Counter对象工作的原因是它在构造函数和析构函数中完成了它的工作。 If you want Widget to use Counter for counting, you need to make sure that Widget 's constructors call Counter 's constructors, and also that Widget 's destructor calls Counter 's destructor. 如果你想让Widget使用Counter进行计数,你需要确保Widget的构造函数调用Counter的构造函数,并且Widget的析构函数也会调用Counter的析构函数。

You can achieve this by making Counter an instance member of Widget . 您可以通过使Counter成为Widget的实例成员来实现此目的。 Even though Widget never accesses Counter 's methods explicitly, C++ makes sure of calling Counter 's constructor and destructor implicitly, as long as Counter is a data member of the Widget class. 即使Widget从不显式访问Counter的方法,C ++也会确保隐式调用Counter的构造函数和析构函数,只要CounterWidget类的数据成员即可。 Note that making Counter a static member of Widget would not have achieved the same goal, because constructor and destructor calls on the instance of Widget are not routed to its static data members. 请注意,使Counter成为Widgetstatic成员不会达到相同的目标,因为Widget实例上的构造函数和析构函数调用不会路由到其static数据成员。

The real solution is a bit down the line in the article. 真正的解决方案是文章中的一点点。 The code you posted only serves the end goal as a means of illustration. 您发布的代码仅作为说明手段的最终目标。 That is not the real solution. 这不是真正的解决方案。

The real solution is to make Counter a class template. 真正的解决方案是使Counter成为一个类模板。

template <typename T>
class Counter {
public:
    Counter() { ++count; }
    Counter(const Counter&) { ++count; }
    ~Counter() { --count; }

    static size_t howMany()
    { return count; }

private:
    static size_t count;
};

template <typename T>
size_t Counter<T>::count = 0; 

And then, use it as a parent class of classes for which you want to count the objects. 然后,将其用作要为其计算对象的类的父类。

// inherit from Counter to count objects
class Widget: public Counter<Widget> {    
    .....
};

I am adding this as an extension. 我将此作为扩展添加。 This does not appear in the article you linked to. 这不会出现在您链接的文章中。

// inherit from Counter to count objects
class Foo: public Counter<Foo> {    
    .....
};

Now, both Widget and Foo have the functionality. 现在, WidgetFoo都具有这些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM