Effective C + + (1)------------------------------Construction/Destruction/assignment

Source: Internet
Author: User

Today began to see effective C + +, feel good writing, hereby in their own language record a deeper impression ~

1. Learn what functions the C + + compiler will write and call by default

When declaring an empty class, the compiler silently writes 4 functions for the class and calls them to implement the function of the class a{};//with the support of the compiler, the empty class above is equivalent to class a{public  :      A () {};   Default constructor      A (const a&) {};//copy constructor      a& operator= (const a&) {};//Copy assignment operator      ~a () {};  destructor};

2.c++ allows a pointer to a parent class to point to the derived class of the parent class, but does not allow pointers to a derived class to point to its parent class, which can result in undefined results.

Class a{public  :      int A;  }; Class B:public a{public  :      int B;}; A * P=new A;   P=new b;//from the above two classes, the memory allocated to the instance of Class A occupies 4 bytes (32bit) and the memory allocated to the instance of Class B accounts for 8 bytes//So when P points to an instance of Class B, the pointer p can only access the first four bytes inherited from b* p2=new B;   P2=new a;//, the result of doing this is undefined.

3.c++ enables polymorphism by introducing virtual functions, so that pointers to the parent class can be used to perform the respective behaviors of each derived class. Also notice that the virtual destructor is declared for the Polymorphic base class (the Polymorphic base class is at least one virtual function in the class, because there is a virtual function that expects to be inherited), and not all classes have to use the virtual destructor, as long as the class is to be inherited (which has virtual functions) using the virtual destructor, The goal is to make the implementation of the derived class customized (there is no memory leak when a derived class object is deleted by a pointer to a base class).

#include <iostream>using namespace Std;class a{public   :            A () {cout<< "a constructor" <<endl;};            ~a () {cout<< "A destructor" <<endl;};            virtual void fun () {cout<< "A" <<endl;};            Virtual ~a () {cout<< "A destructor" <<endl;};}; Class B:public a{public   :       B () {cout<< "B constructor" <<endl;};       ~b () {cout<< "B constructor" <<endl;};       void Fun () {cout<< "B" <<endl;};}; int main () {a   * p=new B;      P->fun ();   Delete p;            Undefined behavior will occur and a virtual destructor must be added to Class A,   return 0;

4. Often said shallow copy refers to the compiler's default generated copy function (essentially copying the memory in the past), if there is a pointer in this class can not be done. We're going to have a deep copy at this point, and the deep copy is that we're rewriting a copy constructor, but note that if we rewrite our copy constructor, the compiler won't tell you when your code is almost wrong.




Effective C + + (1)------------------------------Construction/Destruction/assignment

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.