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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "what are the default member functions of C++ class". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what are the default member functions of C++ class" can help you solve your doubts. Let's follow the editor's way of thinking to learn new knowledge.
The default member function of the class
In each class, if you do not actively implement specific functions, the class will automatically generate these functions, which are the default member functions. There are six default member functions, which are special member functions, and if we don't implement them, the compiler will implement them.
Each of these functions will be described below.
Constructor concept
The function of the constructor is initialization, which is equivalent to the Init function written by ourselves, but it will be called automatically when the object is instantiated to ensure that the instantiated object must be initialized.
Characteristics
(1) the function name is the same as the class name
For example, the date class Date, whose constructor name is also Date
(2) No return value
Take the date class as an example, the declaration and definition of its constructor can be written as follows:
Class Date {public:Date (int year = 0, int month = 1, int day = 1); private:int _ year;int _ month;int _ day;}; class Date {public:Date (int year = 0, int month = 1, int day = 1); private:int _ year;int _ month;int _ day;}; Date::Date (int year, int month, int day) {_ year = year; _ month = month _ day = day;}
(3) when the object is instantiated, the compiler automatically calls the corresponding constructor.
To verify this, we create an object, but do nothing else on it:
Int main () {Date date1; return 0;}
By debugging, you can see that the object date1 has been initialized automatically.
(4) the constructor can be overloaded
(5) if the constructor is not explicitly defined in the class, the C++ compiler will automatically generate a default constructor with no parameters, and once the user explicitly defines it, the compiler will no longer generate it automatically.
(6) both the no-parameter constructor and the full default constructor are called default constructors, and there can only be one default constructor.
With regard to the fifth point, we can't help but wonder what the default constructor automatically generated by the compiler does.
First of all, you need to understand that C++ divides types into two categories: built-in types (basic types) and custom types.
Built-in types are types that come with the C language, such as int, char, double, pointers, and arrays of built-in types, while custom types are defined by strucrt and class.
The constructor generated by the compiler by default does not initialize the built-in type in the member variable, calls its default constructor initialization for the custom type member variable, and reports an error if there is no default constructor.
The concept of destructor function
The function of the destructor is to clean up the resources in the object. The fictional function is called after the life cycle of the object has expired.
Characteristics
(1) the name of the destructor is preceded by the character ~.
(2) No parameters, no return value.
The declaration of the date class destructor can be written as follows:
~ Date ()
(3) A class has one and only one destructor. If the feed is explicitly defined, the system automatically generates a default destructor.
(4) at the end of the object's life cycle, the C++ compiler automatically calls the destructor.
For the default destructor automatically generated by the compiler, similar to the constructor, the built-in type member variable is not handled; for the custom type member variable, its constructor is called.
Copy the concept of constructor
The copy constructor is used to initialize instance objects with objects of the same type.
Features
(1) copy constructor is an overloaded form of constructor.
(2) the parameter of the copy constructor is only one and must be passed by reference. Using the method of passing value will cause infinite recursive calls.
The declaration of the date class copy constructor needs to be written as follows:
Date (Date& date)
Why is it necessary to pass parameters by reference? What about causing infinite recursion?
Consider that passing parameters is actually a temporary copy of the content, and passing the argument to the parameter itself requires calling the copy construction. As a result, copy constructs are constantly called, resulting in infinite recursive calls.
(3) if not explicitly defined, the system will generate a default copy constructor.
The default copy constructor completes a byte-ordered copy (shallow copy) for built-in type members, writes its own copy constructor if a deep copy is required, and calls its copy constructor for custom type members.
The so-called byte-order copy refers to copying and copying the contents of the memory. Errors may occur in some cases, such as pointers that store the memory address of the application. If the pointers are copied in byte order, the member variables in the two objects will point to the same block of space, which will cause the same block of space to release memory multiple times during deconstruction, causing an error.
Assignment operator overload operator overload
Before we talk about operator overloading, we must first talk about operator overloading.
We know that for the int type, it is possible to add, subtract, multiply and divide. But for custom types, is it possible to add, subtract, multiply and divide? This requires the application of operator overloading.
Operator overloading is a function with a special function name. The function name is: operator operator (argument list)
Note: some operators do not support overloading, namely:. *,::, sizeof,?:,.
Taking the overloading of the = = operator of the Date class as an example, you need to use the function as a member function to facilitate access to member variables:
Class Date {public: Date (int year = 0, int month = 1, int day = 1); bool operator== (Date& date) const;private: int _ year; int _ month; int _ day;}; bool Date::operator== (Date& date) const {return _ year = = date._year & & _ day = = date._month & & _ day = date._day } assignment operator overload
Assignment operator overloading, as the name implies, is an overload of the assignment operator that is used to assign copies of two existing objects.
The assignment operator contains four main points:
(1) Parameter type
(2) return value
(3) check whether you assign a value to yourself.
(4) return * this for continuous assignment
If a class does not explicitly define an assignment operator overload, the compiler generates one itself, which is copied in byte order for built-in members, and its assignment operator overloading is called for custom member variables.
Fetch address and fetch address operator overload
These two operators generally do not need to be redefined, but can be overloaded using compiler-generated overloads, and only in special cases, such as when you do not want your specific content to be accessed by others.
After reading this, the article "what are the default member functions of C++ class" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.
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.