(Continue to the expected period)
"Empty discussion is bored." SolmyR smiled, "It's better to set a simple scene to see how your counter is used. Suppose you are the programmer of Blizzard, to design a program for StarCraft Indicates the unit of the people, then the simplest solution is - ", SolmyR stopped, looking to Zero.
ZERO is a breather - this problem is not difficult. He sorted his ideas in the brain: "The company's unit should be designed as a base class, then each particular arousing is derived from this class, each unit is the object of such a class." Thinking here, he flying in hundreds Write down on the board:
Class ProtossUnit
{
......
}
Class Probe: Public ProtossUnit
{
......
}
Class Zealot: Public ProtossUnit
{
......
}
Class Dragoon: Public ProtossUnit
{
......
}
SolmyR nodded and then said: "Very good. Next, we all know that every unit in the interscience is to take up the population, that is, we have to know the number of units, it is obvious, this is an object. Counting the application. So how do we take advantage of the counter you just achieved? "
ZERO is written on the whiteboard.
Class Probe: Public ProtossUnit
{
......
Counter
}
Class Zeolot: Public ProtossUnit
{
......
Counter
}
Class Dragoon: Public ProtossUnit
{
......
Counter
}
wrong! !
Zero's heart has been awarded: this feels too familiar! Almost everywhere suffering from Solmyr toxic, there is this feeling! He can feel that Solmyr is looking for things to smash him. There must be no place!
Going back to see what you wrote, Zero quickly discovered his own mistakes: Counter
"Since it is shared, then you should add it to the base class." Zero hurriedly wiped the above two lines of code, write down:
Class ProtossUnit
{
......
COUNTER
}
Also ... still is not! ZERO immediately discovered the problem: Different arms may not occupy the number of population, like Probe only occupies a population, and Zealot and Dragoon will take two, this ... this ......
ZERO once again wiped the code that just wrote, standing before the whiteboard. At this time, SolmyR sounded: "What's wrong? Is it difficult?". At this point, the smile on the SolmyR face is particularly evil.
"No, I just unclear how the population in the StarCraft is defined, I have never played this game.", Zero tried to delay a little time.
"Yes? Yesterday I also heard you in discussing 'StarCraft God Tactics'? And you wrote the name of the three Shenki arms, spelling accurate." SolmyR is easy to poke Zero lie. . "...", Zero could not help but get annoyed. "What should I do? Let them share a counter, and the count value of each arousing must be different ... is right!" Zero's mind flashed, write down the following code:
Class Probe: Public ProtossUnit
{
......
COUNTER
}
Class Zeolot: Public ProtossUnit
{
......
COUNTER
COUNTER
}
Class Dragoon: Public ProtossUnit
{
......
COUNTER
COUNTER
}
"Yeah! OK!", Zero shouted, completely with a smile in Thai - such interesting scene has become one of the famous entertainment in the company. "The key to sharing a counter is which category as a template parameter! Not necessarily as a template parameter, you can use all kinds of base classes!"
"That count value is not 1?"
"Put a few more counters!"
"Well, it's not bad."
ZERO is very happy to see SolmyR to play a hook on the whiteboard, but joy only maintains a moment - SolmyR is playing a point on the hook.
"Why do you have a point?", Zero is dissatisfied.
"Because your counter is designed, imagine Carrier, it accounts for 8 people, are you going to write 8 Counter members in the Carrier class? Or declare an array of Counter? Is this a statement clear? Is it easy to read? "
"Uh ……"
"And so that use Counter member variables, you need to count the target to pay more at room, for small objects, size or even double."
"Ok ……"
"Take further, the count value is an object, which needs to construct N Counter objects, and running performance is also affected."
"what …… "
"Now you talk about it, how to improve your counter, don't need to change the original customer code?"
"Oh …… "
ZERO is caught in contemplation: Improved counters should have the ability to specify the value, this ability should be ... should be corresponding to a counter object rather than the entire counter class, because the class of sharing the same counter may be different, That is to say, you need to specify a parameter for the object of the counter class ... ah! It turns out as simple!
"I know! The answer is the constructor!", Zero quickly changed the definition of the counter class (formerly definition, see the previous issue):
Template
Class Counter
{
PUBLIC:
Counter (int Ste) // Change section
{{
M_STEP = STEP;
M_count = m_step;
}
~ Counter () {m_count - = m_step;}; // Change section };};
Private:
Static int m_count;
int m_step; // new part
}
"Well, yes, but there is a problem.", SolmyR said, "This, the customer code previously written before using the COUNTER class can not be compiled - they will report less than a parameter when constructing. "
"This is good.", Zero quickly found out what he missed. He changed the definition of constructor to:
Counter (int step = 1)
"This, the previous customer code will default to the count value 1, just like before."
"Well, it is good, but ..."
Zero is tight.
"Forget it, this is this today."
"Yeah!"
"Take these discussions today into detailed documentation, and give me it before get off work."
"Ah! ~~~~"
..................
In this way, once again, the story is over the tragic sound of Zero.