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 on the 'object count', the speaker is Zero."
ZERO: "What ... What ?! Wait, should this lecture not talking about SolmyR ?!"
Moderator: "Well, the original is from SolmyR, but there is something to go out, before leaving him, he specifies your top. He didn't tell you?"
ZERO: "He didn't make it with me! I ... I haven't done anything! How is this? Don't be a joke ?!"
Moderator: "You don't have to be modest, SolmyR said to me before I left you fully competent this issue. Ah, there is a note he left for you."
ZERO opened the tail, but saw the above: "" 50 "(Note:" More Effective C 2 / e "book) See how it? If you have seen it seriously, there is no problem. If you dare to refuse Or you have a saucer, 嘿嘿 ... "
..........
"Hey!", Zero's sighs, "Faced with reality, hard scalp!" He decided to talk about the easiest part, anyway, put this scene into the past. 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 current object, and uses the constructor and the destructor to increase its value, like this:"
Class Wedget
{
PUBLIC:
Wedget () {m_count ;};
~ Wedget () {m_count--;};
Int getcout () {return m_count;};
Private:
Static int m_count;
}
INT WEDGET :: m_count = 0;
Said, Zero found this thing seems to be actually not so difficult, but feel gradually entering the state, and the words are fluent:
"The above practice is easy to understand: The member variable of the Static type in a class is shared by all objects of this class. When this class adds an object, the constructor guarantees the count value to add one, when an object is destroyed, The destructor will ensure that the count value is reduced. One thing to pay attention to here: If the Wedget is derived from a base class, then the destructor of the base class must be declared as a virtual function. Why? Because we often use the base class The pointer operates the object of derived class, which is one of the basic technologies of the "polymorphic" practice, one of the basic technologies for object-oriented programming. This means that the code below will be common: "
Class Base
......
Class Wedget: Public Base
......
Base * pb = new wedget; // base class pointer points to derived class object
......
Delete PB;
"But if Base's destructor does not declares as virtual functions, then when executed to Delete PB, the compiler only knows that PB is a Base * type pointer, only calls the sect of the Base class, so As soon as I destroyed a Wedget class, the destructor of the Wedget class did not call, and the count value will have an error. So you must declare the base-based function to declare the virtual, tell the compiler to determine the actual actual situation of this object. Type to ensure that the destructor of the WEDET class is called. "Zero has a meal, continued:
"By the way, this is a universal principle of C object-oriented programming."
Zero came to an eye on the stage, found that everyone listened very serious, some people also showed the expression of comprehension, which made his confidence increased, decided to follow:
"In a sense, now we have solved the problem of 'object count'. But things have not been completed - we may have many classes need to count objects, if we add this to each class like this The code goes in, then this job is boring and easy to make mistakes, so we need a generic mechanism. The simplest, of course, to 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 member of a Counter in the class that needs to be counted, like this:"
Class Wedget
{
......
Counter M_Mycounter;
}
"This way, add a Wedget object to add a Counter object, destroying a Wedget object, destroying a Counter object, looks perfect. But ...", Zero dragged a long sound, "Solution It is wrong! "After finishing, Zero exaggerated on the whiteboard.
Seeing the people under the stage, Zero is very satisfied with the performance of their own behavior, and he is proud of the explanation:
"Because Static members are shared by all objects that are shared by this class, 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! Please note that both the M_MyCounter member of the Wedget and other M_MyCounter members are objects of the Counter class, they share the same m_count static variable. "
"OK, to bypass this problem, you must use a little small means, that is the template:", Zero writes the following code on the whiteboard:
Template
Class Counter
{
PUBLIC:
Counter () {m_count ;
~ Counter () {m_count--;
Int getcout () {return m_count;};
Private:
Static int m_count;
}
Template
INT Counter
{
......
Counter
}
Class Other
{
......
Counter
}
"It is seen that there is two classes, and there are two classes, so their m_count is independent, so we have achieved different classes of each independent count."
ZERO turned, surprised to see SolmyR I don't know when it appeared on his seat, the mouth is carrying - what? Didn't you see it? Zero found that it is not a SolmyR signature, but a support, thumbs up smile, Zero can't believe his eyes. However, in turn, SolmyR's expression is again switched back to Zero's familiar model - fast, people think that the simplicity you have seen is illusion - Zero's heart is sinking, knowing something is somewhat not, really -
"I will come to a question.", SolmyR spoke, and smiled very brilliant ...
(to be continued)