(C ++ conversation) SolmyR's small piece of textual series: object count (below)

zhaozj2021-02-17  49

Author: Solmyr

(Continued on the futures) "The empty discussion is bored." SolmyR smiled, "Not as good as we set a simple scene to see how your counter is used. Suppose you are the programmer of Blizzard, The StarCraft design program represents the unit of the monk, 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: 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. The application of the count. So how should we take the counter you just implemented? "Zero wrote on the whiteboard: Class probe: public protossunit { ...counter m_mycounter;}; class zeolot: public: public Protossunit {...counter m_mycounter;}; class dragoon: public protossunit { ...                                                           ! 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 and Counter are different classes, their counting values ​​are independent, while the armed populations of each soldier in the StarCraft It is shared. "Since it is shared, then it should be added to the base class." Zero hurriedly wiped the two lines of code, write: class protossunit { ...   Counter m_mycounter;}; ... still is not right! 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 it again The following code, standing on 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? You have to share a counter, and each of the counts must be different ... is right!" Zero's mind flashed, write down the following code: Class Probe: public protosunit { ...   Counter m_MyCounter;}; class Zeolot: public ProtossUnit { ...... Counter m_MyCounter; Counter m_MyCounter;}; class Dragoon: public ProtossUnit { ...... Counter m_mycounter; Counter m_mycounter;}; "Yeah! OK!", Zero is happy, completely with a smile looks down - such interesting scene has become a company One of the famous entertainment. "Which category is used as a template parameter! It is not necessarily used as a template parameter, which can be used as a common base class!" "" The count value is not 1? "" Put a few more counter ! "" Well, it is good. "Zero was very happy to see SolmyR to play a hook on the whiteboard, but joy only maintained a moment - SolmyR moved to 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? "Hey ..." and then use Counter member variables, and the object that needs to be counted will pay more at space. For small objects, the size may even double. "" Well ... "" further The value of the count value is N, which needs to construct N Counter objects. "" Ah ... "" Now you talk, how to improve your counter, don't need to change the original customer code? " "Oh ..." Zero 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 same counter The count value is 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 is fast to change the definition of the counter class (for the previous definition): Template Class Counter {public: Counter (int STEP) / / Change part {  ma_step = step; m_count = m_step;}; ~ counter () {m_count - = m_step;}; // Change section    }; ) {RETURN M_COUNT;}; private int m_count;                                                                                        

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

New Post(0)