c++--when you need to define an assignment/copy constructor __jquery

Source: Internet
Author: User
Tags function definition

Inheritance and dynamic memory allocation

Assuming that the base class uses dynamic memory allocations, and that destructors, copy constructors, and assignment functions are defined, but dynamic memory allocations are not used in derived classes, you do not need to display the definition destructor, copy constructor, and assignment function in a derived class.

When a base class and a derived class adopt dynamic memory allocation, the destructor, copy constructor, and assignment operator of the derived class must use the corresponding base class method to handle the base class element. This requirement is met in three different ways. For destructors. This is done automatically, which means that destructors that call the base class do not need to be displayed in the destructor of a derived class. For constructors, this is done by calling the copy constructor of the base class in the initialization member list. If you do not, the default constructor for the base class is called automatically, and for the assignment operator, this is done by calling the assignment operator of the base class, which is displayed by using the domain resolution operator.

Compiler-generated member functions

1, default constructor

The default constructor number is either no parameters, or all parameters have default values. If no constructors are defined, the compiler defines the constructor. In addition, if the call base class constructor is not displayed in the member initialization list of the derived class constructor, the compiler uses the default constructor of the base class to construct the base class portion of the derived class object. In this case, if the base class has no constructors, it will cause a compile-time error. If a constructor is defined, the compiler will not define a default constructor. In this case, if you need a default constructor, you must provide it yourself. (The best advice is that once you've defined a constructor, it's best to define a default constructor so that it's easy to invoke in other subclasses)

One of the motivations for providing constructors is to ensure that objects are always properly initialized. In addition, if your class contains pointer members, you must initialize those members. Therefore, it is best to provide a display default constructor that initializes all class data members to a reasonable value.

2. Copy constructor

The copy constructor accepts the object of its owning class as a parameter. The copy constructor is used in the following situations

Initializes a new object to a homogeneous object

Passing an object to a function by value

function returns an object by value

compiler generates temporary objects

If the program does not use a copy constructor, the compiler will provide the prototype, but not the function definition, otherwise the program will define an assignment constructor that performs member initialization. In other words, each member of the new object is initialized to the value of the corresponding member of the original object. If the member is a class object, the copy constructor of the corresponding class is used when the member is initialized.

If a member pointer initialized with new is typically required to perform a deep copy, or the class might contain static variables that need to be modified. In these cases, you need to define your own copy constructors.

3. Assignment constructor

The default assignment operator is used to handle assignments between similar objects. Do not confuse assignment with initialization. If the statement creates a new object, the initialization is used, and if the statement modifies the value of an existing object, it is assigned.

The default assignment is to assign a value to a member. If the member is a class object, the default member assignment uses the assignment operator for the corresponding class. If you need to display the definition copy constructor, it is based on the same reason. You also need to display the definition assignment operator.

For a derived class, a protected member is similar to a shared member, but for the outside, it is similar to protecting a member from a private member. A derived class can access the protected members of the base class directly, but only through the member functions of the base class to access the private member.

The destructor of a base class should be virtual. Thus, when a derived object is deleted by pointing to the object's base class pointer or reference, the program first invokes the destructor of the derived class and then calls the destructor of the base class, not just the destructor of the base class

How to judge the true type of the object that a pointer points to, using the run-time mechanism in C + +, typeID can achieve the goal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.