In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "when to use class in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
C.2: classes containing invariants use class to define classes, and struct to define classes if data members can be changed independently.
Translator's note: an invariant can be regarded as a condition that a member of a class must meet. For example, for std::string, the length member must be equal to the length of the string it manages.
Reason (reason)
Readability. Reduce the difficulty of understanding. Using the class keyword makes programmers aware of the need for invariants. This is a useful practice.
Translator's note: the use of the class keyword also ensures by default that data members will not be modified at will, which makes it possible to maintain invariants.
Note (Note)
Invariants are the logical conditions that members of an object must satisfy. These conditions are established by constructors and are the prerequisites of public member functions. Once the invariant is established (usually by the constructor), all member functions of the object can be called. Invariants can be informally specified (for example, through comments) or formally checked by Expects (antecedent conditions).
If all data members can be changed independently of each other, there can be no invariants.
Example (sample)
Struct Pair {/ / the members can vary independently string name; int volume
}
But:
Class Date {public: / / validate that {yy, mm, dd} is a valid date and initialize Date (int yy, Month mm, char dd); / /... private: int y; Month m; char d; / / day}
Translator's note: the invariant of this example is that the combination of year, month and day must be valid.
Note (Note)
If a class contains private data members, the consumer cannot fully initialize the object of the class without using the constructor. Therefore, the definer of a class must define its meaning while providing the constructor. This actually means that the definer needs to define invariants.
See also (see):
Define a class with private data as class
Use the class keyword to define a class that contains private data.
Prefer to place the interface first in a class
It is recommended that you define the interface at the beginning of the class
Minimize exposure of members
Minimum exposure of members
Avoid protected data
Avoid protected type data
Enforcement (implementation recommendations)
Look for structs with all data private and classes with public members.
Find all data structures that are defined as private and classes that contain public members.
Translator's note: the so-called principles are those good practices that can be implemented without deep thinking, but the effect will gradually become apparent.
This is the end of the content of "when to use class in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.