The Calling sequence of the constructor of the C ++ aggregate relationship class, and the aggregate Constructor

Source: Internet
Author: User

The Calling sequence of the constructor of the C ++ aggregate relationship class, and the aggregate Constructor

Indicates an aggregation relationship.

The following code is used to implement

1 # pragma once2 class Engine3 {4 public: 5 Engine (); 6 ~ Engine (); 7}; Engine. h 1 # include <iostream> 2 # include "Engine. h "3 using namespace std; 4 5 Engine: Engine () 6 {7 cout <" Call constructor: Engine () "<endl; 8} 9 10 Engine: :~ Engine () 11 {12 cout <"Call destructor :~ Engine () "<endl; 13} Engine. cpp 1 # pragma once 2 # include "Engine. h "3 4 class Car 5 {6 public: 7 Car (); 8 ~ Car (); 9 private: 10 Engine m_eCar; 11}; Car. h 1 # include <iostream> 2 # include "Car. h "3 using namespace std; 4 5 Car: Car () 6 {7 cout <" Call constructor: Car () "<endl; 8} 9 10 Car: :~ Car () 11 {12 cout <"Call the Destructor :~ Car () "<endl; 13} Car. cpp

From this we can see that the calling sequence of object constructor is the same as that of building a car. We can build an engine and other components before assembling a car, the calling of the Destructor is the same as disassembling a car. First, the outermost is opened, and then the inside is pulled.

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.