In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the classes and objects of C++". In the operation of actual cases, many people will encounter such a dilemma. Then 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!
one。 Then look at the constructor.
We have learned the basics of constructors before, so here we will take a closer look at constructors.
1. The initial value class Date {public: Date (int year, int month, int day) {_ year = year; _ month = month; _ day = day; / / can be assigned multiple times, but generally not _ year = 1;} private: int _ year; int _ month; int _ day;}
First of all, we cannot call it initialization for assignments in the constructor body. First of all, we have to understand that initialization can only be initialized once, and the constructor body can be assigned multiple times. So when is the initialization of object member variables performed? This is what the initialization list needs to do next.
two。 Initialization list
The initialization list starts with a colon, followed by a comma-separated list of data members, each "member variable" followed by an initial value or expression in parentheses. It takes the following form:
Class Date {public: Date (int year = 0, int month = 1, int day = 1): _ year (year), _ month (month) {_ day = day;} private: int _ year; int _ month; int _ day;}
1. Each member variable can only appear once in the initialization list (initialization can only be initialized once)
two。 The class contains the following members, which must be initialized in the initialization list:
(1) const member variable: since the const variable cannot be changed after initialization, it needs to be initialized in the initialization list.
(2) reference member variables: reference member variables can only be used as references to one variable, and once initialized, they can no longer be used as references to other variables, so reference variables can only initialize the list again.
(3) Custom type member variables (without default constructor): since custom type variables cannot be initialized without a default constructor, and the program cannot be compiled, custom type member variables without a default constructor must be initialized in the initialization list.
Class B {public: B (int I): _ I (I) {} private: int _ I;} Class A {public: a (int a, int& b, int bb): _ a (a), _ b (b), _ bb (bb) {} private: const int _ int& _ int& / reference member variable B _ bb;// Custom member variable}
3. Use initialization lists whenever possible, because for custom type member variables, be sure to use initialization lists first, whether or not you use initialization lists. For example, the execution result of the following code:
Class B {public: B () {cout
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.