C + + constructor and BC credit disk platform Rental learning experience

Source: Internet
Author: User

BC Credit disk Platform rental Q1446595067 Some basic points of knowledge:

constructor function:

对象生成时构造函数自动被调用,对象一旦生成,就在也不能在其上执行构造函数。若自己定义的构造函数带参数,则声明对象时也需要带参数,缺省函数除外。

There are three scenarios in which the copy constructor works:

当用一个对象去初始化同类的另一个对象时 如 Test a1=a2,区别于赋值 Test a1,a2;a1=a2;如果某函数有一个参数是类 A 的对象,那么该函数被调用时,类A的复制构造函数将被调用;如果函数的返回值是类A的对象时,则函数返回时,A的复制构造函数被调用。

Three scenarios in which C + + produces temporary objects:

类型转换;以值的方式给函数传参(可对应上面三种情况的2);注意如果是按引用传递,则不会生成临时变量。函数需要返回一个对象时(对应3)。

Here is a concrete example:

#include <iostream> #include <windows.h>using namespace Std;class crectangle{private:int w,h;              static int ntotalarea;      static int ntotalnumber;             Public:crectangle (int w_,int h_);             ~ Crectangle ();             static void Printtotal (); Crectangle AddOne ();};                           Crectangle::crectangle (int w_,int h_) {w=w_;                           H=h_;                           Ntotalnumber + +;                           Ntotalarea +=w*h; cout<< "constructor called" &LT;&LT;ENDL;}                         Crectangle::~crectangle () {ntotalnumber--;                         Ntotalarea-=w*h; cout<< "destructor called" <<endl;           }crectangle Crectangle::addone () {this->w + +; return *this;} void Crectangle::P rinttotal () {cout<<ntotalnumber<< "," &LT;&LT;NTOTALAREA&LT;&LT;ENDL;} int Crectangle::ntotalNumber=0;int Crectangle::ntotalarea=0;int Main () {crectangle R1 (3,3), R2 (2,2); R2=r1.        AddOne ();        Crectangle::P rinttotal (); R1.          Printtotal ();        System ("pause");     return 0; }

The operation results are as follows

In the execution r2=r1. AddOne (), the copy constructor is called to generate a temporary hidden Crectangle object because the AddOne function returns the object type. The temporary object, when it dies, calls the destructor, reducing the values of ntotalnumber and Ntotalarea, but the temporary objects do not increment their values at the time of generation, so the result is not 2, 13, which becomes 1, 1. To avoid the effects of temporary variables, you can write a copy constructor yourself for crectangle, changing the values of Ntotalnumber and Ntotalarea inside.

CRectangle ::CRectangle(CRectangle & r){           w=r.w;           h=r.h;           nTotalNumber ++;           nTotalArea +=w*h;           cout<<"copy constructor called"<<endl;}

But compile error

After careful examination, it is found that the function AddOne return type error and does not match the type of parameter required by the copy constructor.

Modified before Crectangle Crectangle::addone () Modified Crectangle & Crectangle::addone ()

Run results

As you can see, the copy constructor is not called because the AddOne function return type is a reference, not an object type.

Note that if you modify the main function as follows

int main()    {        CRectangle r1(3,3),r2(2,2),r3=r1.AddOne();//调用AddOne函数对r3初始化        CRectangle::PrintTotal();        r1.PrintTotal();          system("pause");        return 0;      }

The result of the operation has changed

C + + constructor and BC credit disk platform Rental learning experience

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.