In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
C++ object-oriented class design, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
The design of the class is to fully express a concept of clear responsibility with just the right information, which means that no more and no less, the concept is incomplete; more, it is redundant and cumbersome, of course, in special cases, a little repetition is allowed, but there must be a good reason. Redundancy often means that it contains too much information, and the expression of concepts is not accurate enough, such as goto, pointers, inheriting more of these goods, it is because of its excessive connotation that its use should be strictly restricted. For example, as said on more effective C++, the member function of class should be kept to a minimum in a complete case. However, our starting point here is the complete minimization of member data.
The advantage of minimization is that it can maintain the maximum independence of the concept, which means that the concept can be implemented at a minimum cost, and that it requires less code in the application layer and is non-intrusive. It's like centering 11 noexcept instead of throw (), like separating the concept of an interface from multiple inheritance, like a normal member function pointer that doesn't take into account the virtual inheritance of multiple inheritance. For example, if you don't require a read-only string to end with 0, you can treat any part of a read-only string as a read-only string. The external function of the class is important, but so is what the class cannot do.
First of all, it is necessary to have a clear concept and the most basic operations to be supported by this concept, and then organize the data on this basis to minimize the member data. Of course, the concept is not conceived with a pat on the head, but because there are too many times of that kind of related data in the code, so it is necessary to package the data and abstract them into a concept. For example, when you see iterators that start and end function parameters of the stl algorithm everywhere, it is necessary to put the start and end together. For example, the emergence of string_view, which assumes that its character storage type is char,string_view, means contiguous char memory blocks, which can be expressed as follows.
Struct string_view {const char* textBegin; size_t length; / / or const char* textEnd}
The point here is that the two member fields in string_view are indispensable, but there is no need to add anything else. Then, expand and implement a series of member functions on these two data, here, member functions and member fields are a little tangled, because the necessary set of member functions (refinement of the original concept), the member function determines the representation of the member field, and after the member field is determined, this in turn verifies the necessity of the member function. In any case, the design of member functions must also follow the principle of minimum integrity. To be more specific, that is, whenever a member function can be implemented by other member functions, it means that the function should be driven out of the class and exist as a global function. Of course, this is not a rigid dogma, some very special functions, can also be member functions, because the use of member functions is indeed very convenient.
It may be confusing to feel that all member functions can actually be global functions. In other words, we can make a pair of set and get functions for each member field, then all other member functions can be in the form of global functions, which can easily follow the principle of minimum integrity. Of course, this is obviously a bad behavior of being lazy and refusing to think. Instead of doing this, you might as well open all the member fields and fall into the pattern of the c language. So the legal theory is, a function, which is assumed to be a global function, if its implementation must access the member field, can not be achieved by calling the member function of this class (usually not get,set), or, you can forcibly use other functions to complete the task, but it is troublesome, or you have to pay a price in time and space, then it means that this function should be a member function of this class.
The design of the class is the essential member fields and essential member functions, together, they achieve a complete expression of the original concept of the class, regardless of anything else. If a class is not easy to write, it often means that the function of the class is not specific, or its concept is incomplete, at this time, you can not rush to abstract, if it is necessary for a class to be born, then in the writing of the code, the abstract concept of this class will be repeated again and again, and the ape's understanding of it will become clearer and clearer, so that it can be created naturally. All non-demand-driven, non-code-driven, clapping on the head, taking it for granted, is an abstract, out of touch with real-life art, and will eventually be eliminated.
Class design, its focus is to use the necessary data to fully express a clear concept. Inheritance, on the other hand, refines the concept of classes, that is, classification, for example, animals and plants are divided into two subcategories, that is, animals and plants. Inheritance is different from the classification of daily life. The classification of inheritance is open, and new subcategories can be added at any time as needed. The whole class system is a strict single-root tree, and any class can only have one root class. Starting with any class, there can only be one path back to the original root class, java, C# is Object, all classes are derived from Object, which is a big tree. Under a single root, everything is an object, which is naturally very convenient. At the very least, it directly supports the junk any of C++ std from the language level. Because of the perfect reflection information of java and C#, we can also do things at the dynamic language level by abandoning static type information, while all the dynamic type information of void*, of cPeragram + is in the brain of apes. There are a lot of dynamic languages on the java platform, and the performance is very good.
Relative to many languages, C++ is a freak and an anomaly, it has its own design philosophy, it is multi-root, it is impossible and unnecessary to become a single root, of course, we can assume an empty class, and then all classes inherit from this empty class by default. All the classes of C++ make up a forest in which trees grow from the earth. But in any case, only single inheritance can be allowed, and there must be no more inheritance. This is the bottom line and must not be violated. (of course, on occasions with obscene tricks and tricks, you don't have to abide by this commandment. Inheriting more is not necessary, but it is indispensable. Because it can play a lot of tricks, and they are all very practical and necessary). At the very least, the single root memory layout is intuitive and predictable, to a certain extent, across compilers, only a good memory layout, good binary reuse is expected. In addition, the parent class knows nothing about the subclass, do not refer to the slightest bit of information about the subclass, but keep the information one-way fluid.
In this single-root hierarchical class system, everything is dead, without a little bit of social vitality. Obviously, only categories that belong to the same parent class can share such a pitiful bit of commonality. If there is no interface to make trouble, what will be a tragedy, the best example, mfc, really amazing, did not use the interface, can be made to strictly meet most of the needs of the gui framework, there is no interface, does not mean that it does not need, because mfc opened the back door, using a more powerful thing-message sending, even so, some places of mfc base classes still rely on subclasses, which is very speechless.
Under C++, the design of the class is absolutely not childish, you must be clear about what you want, so that the abstract concepts will not become rubbish. Large and comprehensive classes are far less than a few small and specialized categories. Java,C# class development is very convenient, but the granularity is too large, throw a package of things to you, forced to sell and buy, anyway, as long as the class is defined, there must be a large pile of perfect reflection information, and the object also contains some unimportant member fields, and the object access is all indirect reference access, although now the computer performance is excessive, these are all innocuous. C++ gives developers the biggest choice, and apes who make C++ are basically overintelligent, and for each choice, they are clear about the cost behind and the purpose they want to achieve, so although there is a mental burden that affects development efficiency during development, however, there is no guilt about performance baggage. Personally, I still like C++, the language with the highest degree of freedom. Sometimes, for the most meticulous control of memory, we can get a more streamlined design, which has nothing to do with running performance. For example, in C++, as long as the memory layout is consistent, even different types of objects are treated uniformly through forced type conversion, and then do strange things, such as in COM, for aggregation and reuse, a class It is surprising that two sets of implementations can be provided for the same interface. This convenience is not supported in other strongly typed languages.
In a sense, the language mechanism provided by C++ on object-oriented is to easily generate various memory layouts and the operations that can be supported on this memory layout. Virtual functions are used to generate a bunch of member function pointers. Inheritance is used to easily generate a bunch of member fields,. Therefore, C++ 's object-oriented design is oriented to the memory layout, and such stupid things as multi-inheritance, virtual inheritance, and templates can easily cause the memory layout to get out of control, but if used properly, it has miraculous effects that other languages do not have. Really, in terms of dynamic performance art, any language is a kindergarten pupil in front of the adult C++.
Because although the class is the foundation, the essence of static object orientation is all on the interface. Only by clearly defining the functional responsibilities of the class can we understand the necessity and diversity of interfaces. So, can there be only interfaces and no classes? Yes, just like com, and the price is that it is inconvenient to use.
After reading the above, have you mastered the methods of C++ object-oriented class design? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.