C++primer 13.1.3 Sessions

Source: Internet
Author: User
Tags sessions

Exercise 13.9

The destructor performs the opposite of the constructor, the constructor initializes the object's non-static data member, the destructor frees the resource used by the object, and destroys the object's non-static data member. When a class does not define its own destructor, the compiler defines a composition destructor for it.

Exercise 13.10

In a constructor, the initialization of a member is done before the function body executes and is initialized in the order in which they appear in the class. In another destructor, the function body is executed first, and then the member is destroyed. Members are destroyed in reverse order of initialization.

Exercise 13.11

1#include <iostream>2#include <string>3#include <memory>4 5 using namespacestd;6 7 8 classHasptr {9Friend Ostream &print (ostream &os, Hasptr &h);Ten  Public: OneHasptr (Const string&s =string()): PS (New string(s)), I (0) {} AHasptr (ConstHasptr &ptr); -Hasptr &operator=(ConstHasptr &PT); -~hasptr () {DeletePS;} the Private: -     string*PS; -     inti; - }; +  -Ostream &print (ostream &os, Hasptr &h); +  A intMain () at { -Hasptr has ("Hello"); -Hasptr has =has ; - print (cout, have); -System"Pause"); -     return 0; in } -  toHasptr::hasptr (ConstHasptr & PTR): PS (New string(*(ptr.ps))), I (PTR.I) {} +  -Hasptr & Hasptr::operator=(ConstHasptr &PT) the { *PS =New string(*pt.ps); $i =pt.i;Panax Notoginseng     return* This; -     //TODO: Insert a return statement here the } +  AOstream & Print (ostream & OS, Hasptr &h) the { +Os << *h.ps << h.i <<Endl; -     returnos; $     //TODO: Insert a return statement here $}

Exercise 13.12

The second parameter of the function copies the object, two objects constructed in the body of the function.

It is important to note that destructors do not execute when a reference or pointer to an object leaves the scope.

Exercise 13.13

1#include <iostream>2#include <string>3#include <memory>4 5 using namespacestd;6 7 structX {8  Public:9X () {cout <<"X ()"<<Endl;}TenX (shared_ptr<int> Q,intn): P (q), I (n) {cout <<"Gouzao"<<Endl;} OneXConstx&) {cout <<"X (const x&)"<<Endl;} AX &operator=(Constx&x_1); -~X () -     { thecout <<"Delete X"<<Endl; -     } - Private: -shared_ptr<int>p; +     inti; - }; +  A  at intMain () - { -X X1;//default constructor -Auto Q = make_shared<int> ( the);//Create a new smart pointer -X X2 (q,3);//constructors with formal parameters -x1 = x2;//Copy assignment operator inX X3 (x2);//copy Constructor -System"Pause"); to     return 0; + } -  theX & x::operator=(ConstX &x_1) * { $p = make_shared<int> (*(X_1.P));Panax Notoginsengi =x_1.i; -cout <<"operator"<<Endl; the     return* This; +     //TODO: Insert a return statement here A}

C++primer 13.1.3 Sessions

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.