(C ++ dialogue) SolmyR's small piece series: object count (on)

zhaozj2021-02-17  51

Author: Solmyr From: pchome.net

The seat under the stage is full, except for the seat of SolmyR. Zero is helpless, looks at the only vacancy, starting the first hundred sighs why they will fall to such an embarrassment. Just a few minutes ago, everything is still normal, until ............ .......... Moderator: "The next agenda, the topic of the C programming technology lecture, the speaker is Zero." Zero: " ... What ?! Wait, should this lecture not talking about SolmyR?! "Moderator:" Well, the original is from SolmyR, but it is time to go out, before he specifically, he specifies you. He Didn't tell you? "Zero:" He didn't mention it with me! I ... I have not prepared! How do I open it? Don't make a joke ?! "Moderator:" You don't have to be modest, Solmyr left before I said that you can be able to compete for this topic. Ah, there is a note he left here. "Zero opened the strips, but see above:" 50 "(Note:" More Effective C 2 / e "one book) What is it? If you have seen it seriously, there is no problem. If you dare to refuse or have a sauce, hey ..." ... ", Zero is sigh, "In the face of reality, it is hard to scalp!" He decided to talk about the easiest part, and throw this scene. He looked at the "object count" on the whiteboard. He said: "Today ... this ... this is the topic of discussion is the 'object count'. The so-called object count ... Ah ... how much is the calculation Object. The opening is white, Zero feels that it is better to turn to the actual thing as soon as possible. "For this problem ... The easiest way is to add a static variable in a class that needs to be counted, saves the number of current objects, and uses the constructor and the destructor to increase its value, like this:" Class Wedget { Public:     {m_count ;};};      {m_count ()};           ;}; stout;  ; : m_count = 0; said that Zero found that this thing seems to be actually not so difficult, but feel that gradually entered the state, and the words are also fluent: "The above practice is easy to understand: a member variable in a class of Static types Shared by all objects of this class. When this class adds an object, the constructor guarantees the count value to add one. When you destroy an object, the destruction function guarantees the count value minus one. The only thing you need to pay attention here is only one thing. If the Wedget is derived from a base class, then the destructor of the base class must declare to virtual functions. Why? Because we often use the base class's pointer to the object of the class, this is the so-called "polymorphological" practice One of the basic techniques of object-oriented programming.

That is to say, the code below will be very common: "Class Base ... Class Wedget: public base ... Base * Pb = new wedget; // base class pointer points to the derived class object ... delete PB;" But if Base The destructor does not declare the virtual function, then when executed to the Delete PB, the compiler only knows that PB is a Base * type pointer, only calls the analysis function of the Base class, so that clearly Destroyed an object of a Wedget class, the destructor of the Wedget class is not called, and the count value will have an error. So you must declare the BASE's destructor as a virtual, tell the compiler to determine the actual type of this object, and ensure that the destructor of the Wedget class is called. "Zero has a meal, continued:" By the way, this is a universal principle of C object-oriented programming. "ZERO has an eye to look at the stage, and found that everyone listened very serious. Some people also showed the expression of comprehension. This made his confidence, decided to follow:" In a sense, we have solved now. The problem of 'object count'. But things have not been finished - we may have many classes need to count objects. If we add these code to each class like this, then this work is boring and easy to make mistakes, so we need a general purpose Mechanisms. The simplest, of course, package the above code into a class: "Class Counter {public :counter () {m_count ;}; ~ counter () {m_count--;} ;int getCout () {RETURN M_COUNT;}; private :static int m_count;}; int counter :: m_count = 0; "Then add a Counter member in those that need to count, like this:" Class Wedget { ... Counter m_mycounter;}; "This, adding a Wedget object adds a COUNTER object, destroying a Wedget object, destroying a Counter object, looks perfect. But ... ", Zero dragged a long sound," This solution is wrong! "After saying, Zero has exaggerated a big fork on the whiteboard. Seeing the people under the stage, Zero is very satisfied with the performance of his own behavior, and he is proud of the explanation:" Because Static members are All objects are shared, so if there is another class, such as the Other class contains a M_MyCounter member, the Wedget and Other class are actually shared a count value! Note that both the M_MYCUNTER member of the Wedget and other M_MyCounter members are objects of the Counter class, they share the same m_count static variable.

转载请注明原文地址:https://www.9cbs.com/read-30545.html

New Post(0)