C ++ Construction and Analysis

Source: Internet
Author: User
  1. # Include <iostream>
  2. Using namespace STD;
  3. Class B
  4. {
  5. Public:
  6. B ()
  7. {
  8. Cout <"default constructor" <Endl;
  9. }
  10. B (B & B)
  11. {
  12. Cout <"copy constructor" <Endl;
  13. }
  14. ~ B ()
  15. {
  16. Cout <"destructed" <Endl;
  17. }
  18. B (int I): Data (I)
  19. {
  20. Cout <"Constructed by parameter" <data <Endl;
  21. }
  22. B & operator = (B & B)
  23. {
  24. Cout <"========" <Endl;
  25. Return * this;
  26. }
  27. PRIVATE:
  28. Int data;
  29. };
  30. B play (B)
  31. {
  32. Return B;
  33. }
  34. Int main ()
  35. {
  36. B (5 );
  37. // Unknown object, constructed by B (INT) first, and then analyzed
  38. B T1 = play (B (4 ));
  39. // Call the constructor B (INT) of B when loading play (5), and construct instance B of B according to parameter 5
  40. // When return B returns, call B's copy constructor to construct a new temporary object and return
  41. // The Return Statement is out of B's scope. At this time, the destructor of B is called.
  42. B t2 = play (T1 );
  43. // Call the copy constructor of B when loading play (T1) to construct object B
  44. // When return B, call the copy constructor of B to construct a new temporary object.
  45. // Then call the destructor of B
  46. T2 = T1;
  47. // Partial objects t1 and t2 exit the scope and call their destructor
  48. Return 0;
  49. }

Output:

Constructed by parameter 5
Destructed
Constructed by parameter 4
Copy constructor
Destructed
Copy constructor
Copy constructor
Destructed
==========
Destructed
Destructed

 

  • A named Automatic object is created every time the program executes its declaration and destroyed every time the program leaves the block (scope) It appears;
  • A free storage object is created using the new operator and destroyed using the delete operator;
  • A non-static member object, as a member of another class object, is also created or destroyed when it is created or destroyed as the member object;
  • An array object is created or destroyed when it is created or destroyed as the array of elements;
  • A local static object is created when it is declared for the first time during program execution, and destroyed once upon program termination;
  • A global object, namespace object, and static object of the class. They are created only once at the beginning of the program and destroyed once at the end of the program;
  • A temporary object is created as part of the expression evaluation and destroyed at the end of the complete expression it appears;
  • An object that is controlled by the provided parameters in the allocation operation and stored in the storage obtained through the functions provided by the user;
  • A union object, which cannot have constructor and destructor.
    (The above part is taken from <C ++ programming language>, p218)

About how to calculate sizeof (class)
Null: 1
No virtual function: Sum of sizeof (data member)
There are virtual functions: sizeof (data member) and + sizeof (V Table pointer) = 4

 

Multiple threads in the same process share code segments (code and constants), data segments (static and global variables), and extended segments (Heap Storage ),However, each thread has its own stack segment. Stack segments are also called runtime stacks, which are used to store all local variables and temporary variables (parameters, return values, temporary constructed variables, etc ).

 

 

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.