Friday, June 12, 2015

Calling another ctor in one ctor


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;


class Widget {
public:
    Widget(int n) {
        data = n;
        cout <<"Widget(int n): this=" <<this <<endl;
    }

    Widget() {
        cout<<"Widget(): this=" <<this <<endl;
        Widget(8888);
    }

    ~Widget() {
        cout<<"~Widget(): this=" <<this <<" data=" <<data <<endl;
    }


    void print() const  {
        cout <<"print(): this=" <<this <<" data=:"<<data <<endl;
    }

private:
    int data;
};


int main()
{
    Widget a;
    a.print();
    return 0;
}

output:

Widget(): this=0x7fff556f9c08
Widget(int n): this=0x7fff556f9b90
~Widget(): this=0x7fff556f9b90 data=8888
print(): this=0x7fff556f9c08 data=:1794977846
~Widget(): this=0x7fff556f9c08 data=1794977846

No comments: