The so-called C ++ Class Object: all objects of a class correspond to the same class object and want to learn C ++ class objects well. First of all, it is very important to understand the concept of the C ++ language. First, let's talk about what is C ++ language, the so-called C ++ language: it is a widely used computer programming language.
Before learning this chapter, we have learned about class constructor and destructor. For common type objects, copying between them is very simple, the objects of the defined classes are also objects. No one can prevent us from copying them in the following ways, for example:
- include <iostream>
- using namespace std;
-
- class Test
- {
- public:
- Test(int temp)
- {
- p1=temp;
- }
- protected:
- int p1;
-
- };
-
- void main()
- {
- Test a(99);
- Test b=a;
- }
Common objects and class objects are the same as C ++ class objects, and their features are similar. class objects have member variables, while common objects do not, when the same replication method occurs on different objects. Then the system performs different operations on them. For Class objects, class objects of the same type are copied through constructors to complete the entire replication process.
- # Include<Iostream>
- Using namespace std;
- Class Test
- {
- Public:
- Test (int temp)
- {
- P1=Temp;
- }
- Test (Test & c_t) // here is the custom copy constructor.
- {
- Cout<<"Enter the copy constructor"<<Endl;
- P1=C_t. P1; // if this statement is removed, the replication cannot be completed.
- }
- Public:
- Int p1;
- };
- Void main ()
- {
- Test a (99 );
- TestB=A;
- Cout<<B. p1;
- Cin. get ();
- }
We didn't see the copy constructor, and we also completed the copy work. Why? Because when a class does not have a custom copy constructor, the system automatically provides a default copy constructor to complete the copy.
Next, to illustrate the situation (for example, the above Code), the C ++ class object defines a copy constructor that is the same as the default copy constructor of the system, let's see how it works internally! Test (Test & c_t) in the code above is our custom copy constructor. The name of the copy constructor must be consistent with the class name, A function form parameter is a reference variable of this type and must be a reference.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code