Conversations: Back to Base-ICSCUJ C Experts Forum: Conversion (September 2001)
As usual, I am a big injury to a very simple design problem. It may be that the Labor Festival holiday has never been able to pay, I feel a little fascinating. Wendy just started to rest, I didn't think it was.
In fact, the task is just to introduce a new category in one project. I already have a class, which is very similar to this class you want to introduce. The problem is, I don't know what relationship should be established between existing classes and new classes. Of course, the new class cannot be inherited from the existing class, because there is no IS-A ("is a") relationship between the two. Layering is not good because I need to override some virtual functions. The remaining choice is only one: either use private inheritance, simulating IS-IMPLEMENTED-IN-TERMS-OF ("Implementation"), or simply extracts their common function design a new base class.
On the occasion of Bes, I think, the prawn still doesn't have more time? I let the turret quickly turn a circle ---- But there is no one around. I feel a little funny, so I still go back to the computer. "That's okay," I speak for myself, "I have to do myself."
"Sometimes it is not so easy." I raised my head, her prawn is staring at me. She continued to asked slowly: "How do you get ready?"
"I am making it for this," I have to admit it, and the heartbeat began to calm down. "SCOTT Meyers' section 40, for another class of existing but conceptually, if you want to use its code, you can use the relationship of is-import-in-terms-of" [Note 1 An example of him is to realize set with list. "I looked at the code to prawn:
Template
"But Article 43 suggests," I will continue, "If two classes enjoy a common code, they have to inherit from a base class. His example is two CartoonCharacter classes, named Cricket and Grasshopper, they all from a new The introduced common base class INSECT inherits. "I gave the second example to prawn:
Class CartoonCharacter {/ * ... * /}; Class Insect: Public CartoonCharacter {/ * ... /}; Class Grasshopper: Public Insect {/ * ... /}; Class Cricket: Public Insect {/ * .. . * /};
Prawns can't help but smile, "Yes. But 43 can only apply to two relationships."
"It's a rookie," How to forget this? I can't help but whispered myself.
"This time I remember, my rookie?" She nodded and added. Some people must be seen around us, and they still have to keep it.
"You are right. But" So I counterattached, "My confusion is that I can use the method of clause 43 to the example 40. That is, do not use the list to implement the set; instead, why not create A new class, such as the base class as a set and list? Take this project, I can design a public base class for new categories and existing classes. "" Oh, I understand "The prawn is nodded," your problem is essentially: how to distinguish between the "Code of Existing Code" and Terms 43 "Sharing Some Code". The surface seems to be very similar "Prawn stopped to think about a moment," I think you should consider this idea:
"Generally, when you want to add a new class to a project, there is already a very similar class existence, there is usually three options: let a class inherit from another class; create a public base class to make new categories Some classes are inherited from it; use a class to implement another class.
"The easiest thing is of course the first case - - Is it IS-A relationship between IS-A? If so, let the new class inherit from existing classes, otherwise it will not."
"Oh, yes," I stepped on the book on the note. "This is what Meyers' points about C must keep in mind. But this time is not 1066! [Note 2]"
The prawn is white, continue to say, "analyze the so-called sharing or public code essence, if you have an existing class code; if there is no code, it is obvious, you can't change the existing class, you can only Using Is-Implement-in-Terms-of. But now this project, you have its code, you have to analyze: Can they be shared? That is, each class really calls the same function ? ---- This doesn't mean the same function, but true the same function. In that example in Meyers, cricket and grasshopper call the same Singcustomization function. They are real public code. On the contrary, if you Introducing a new Container base class, its INSERT function does not necessarily work for Set and List. A class, or even two classes have to override their INSERT functions. In this case, The code is not true public, and it is not good to use the public base class.
"Continue to analyze the above existing categories and new classes. From the concept, do they have a relationship with the base class you think useful? Do you want your Container class to use the standard container class to make an example, never Nothing is called the class of std :: container, and it is impossible to have --- Why? "
"Hey," I have a good, try it, "because the interface is different?"
"This reason is enough." She made me passed. "Their interfaces are different; and the commonality is determined by a specific use occasion, not a specific function name. Just because I want to connect two irrelevant classes to take a base class, this is a bit ridiculous; Instead, IS-IMPLEMENTED-IN-TERMS-OF I should use in this case. "
"Wait a minute," I plugged a sentence. "The INSECT class is not used to contact other two classes?" "Yes, but the Cricket class and the Grasshopper class have contacts - they are derived on the basis of CartoonCharacter. In addition, Insect Class provides some other features, cricket and grasshopper have to build on these features. So, the INSECT class is a very meaningful class. It provides a close, consistent abstraction.
"Pay attention," she continued, "If you want to access the existing protector, you may consider using non-public inheritance.
"Finally, ask yourself: will it bring more inheritance with is-imported-in-terms-of? Better, know, there is no problem with the use of public base classes. Many inheritance is complex, and can use simple methods Why don't you use it?
I feel that I am a bit like I understand, it seems that I have to worry about it. "Well, thank you for your help. I think, I know how to solve myself now!"
"Oh? Yes?" Prawn smashed his head, "Right, I listened to Wendy's husband said ..."
"How?" I excitedly.
"A thousand gold, more than 7 pounds, born at 2:18 Monday, named Jeannine Nancy."
-------------------------------------------------- --- [Note 1] Scott Meyers. Effective C , 2nd Edition, Items 40, 42, And 43 (Addison-Wesley, 1997). [Note 2] Scott Meyers. Effective C , 2nd Edition, Item 35 (Addison-Wesley 1997).