Article 45: Figuring out what C + + is doing behind the scenes for you, called functions

Source: Internet
Author: User

When the C + + compiler passes through it. If you do not declare the following function, the thoughtful compiler declares its own version. These functions are: a copy constructor, an assignment operator, a destructor, a pair of accessor operators. In addition, if you do not declare any constructors, it will also declare a default constructor for you. All of these functions are public. In other words, if you write this:

Class empty{};

It's the same as you write:

classEmpty { Public: Empty (); //default constructorEmpty (Constempty& RHS);//copy Constructor~empty ();//does the destructor----//See descriptions below for virtual functionsempty&operator=(Constempty& RHS);//Assignment OperatorsEmpty*operator& ();//accessor operator  Constempty*operator& ()Const;};

Suppose the compiler wrote a function for you, what do these functions do? So, the default constructors and destructors don't actually do anything, they just allow you to create and destroy objects of the class. Note that the resulting destructor is generally non-virtual (see clause 14) unless the class in which it is located inherits from a base class that declares a virtual destructor. The default accessor operator simply returns the address of the object. These functions are actually as defined below:

 inline Empty::empty () {}inline Empty::  ~ Empty () {}inline empty  * Empty::operator  & () {return  this  ;} inline  const  Empty * empty::operator  & () const  { return  this ; }

As for copy constructors and assignment operators, the official rule is that the default copy constructor (assignment operator) makes a "member-by-unit" copy construct (Assignment) on a non-static data member of a class. That is, if M is a non-static data member of type T in Class C and C does not declare a copy constructor (assignment operator), M will be constructed (assigned) by a copy constructor (assignment operator) of type T----if T has a copy constructor (assignment operator). If not, the rule is applied recursively to the data member of M until a copy constructor (assignment operator) or fixed type (for example, int,double, pointer, etc.) is found. By default, a fixed type of object copy construction (Assignment) is a "bit-wise" copy from the source object to the target object. For classes that inherit from another class, this rule applies to each layer in the inheritance hierarchy, so the user-defined constructors and assignment operators are invoked regardless of which layer is declared.

classnamedobject{ Public: Namedobject (string&name,Const int&value): Namevalue (name), ObjectValue (value) {}Private:    string&Namevalue; Const intObjectValue;};voidMain () {stringNewdog ("Persephone"); stringOlddog ("Satch"); Namedobject p (Newdog,2); Namedobject s (Olddog, in); P=s;}

Article 45: Figuring out what C + + is doing behind the scenes for you, called functions

Related Article

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.