2012年2月15日 星期三

Containment, Initializers, and Default Constructors


The following example demonstrates how constructors work in a container relationship. It also shows how you can control which constructor is called in the contained class by using constructor initialization list syntax.

Example 4-16 – Containment and constructors

1      // File: ex4-16.cpp – containment and constructors
2     
3      #include <iostream>
4      using namespace std;
5     
6     
7      class One
8      {
9          int member;
10      public:
11          One() { cout << "One default ctor called\n";}
12          One(int j) : member(j)
13         { cout << "One second ctor called" << endl;}
14      };
15     
16      class Two
17      {
18          One member;
19      public:
20          Two() { cout << "Two default ctor called" << endl; }
21          Two(int k) : member(k)
22         { cout << "Two second ctor called" << endl;}
23      };
24     
25      int main()
26      {
27          cout << "declare object1" << endl;
28          Two object1;
29          cout << "declare object2" << endl;
30          Two object2(5);
31          return 0;
32      }


******  Output  ******

declare object1
One default ctor called
Two default ctor called
declare object2
One second ctor called
Two second ctor called

Containment vs. Nested classes and constructor calls

The following example illustrates the difference in constructor calls between a container relationship and a nested organization

1      // Example 4-14a.cpp –
2      // Containment vs. Nested classes and constructor calls
3      // Contributed by Raymond White 5/2009
4     
5      #include <iostream>
6      using namespace std;
7     
8      class Innermost
9      {
10        int i;
11      public:
12        Innermost() { cout << "ctor of Innermost\n"; }
13        ~Innermost() { cout << "dtor of Innermost\n"; }
14      };
15     
16      class Inner
17      {
18        Innermost inm;
19        int i;
20      public:
21        Inner() { cout << "ctor of Inner\n"; }
22        ~Inner() { cout << "dtor of Inner\n"; }
23      };
24     
25      class Outer
26      {
27        Inner in;
28        int i;
29      public:
30        Outer() { cout << "ctor of Outer\n"; }
31        ~Outer() { cout << "dtor of Outer\n"; }
32      };
33     
34      class Outermost
35      {
36        Outer o;
37        int i;
38      public:
39        Outermost() { cout << "ctor of Outermost\n"; }
40        ~Outermost() { cout << "dtor of Outermost\n"; }
41      };
42     
43      class NestedOutermost
44      {
45        int i;
46      public:
47        class NestedOuter
48        {
49          int i;
50        public:
51          class NestedInner
52          {
53            int i;
54          public:
55            class NestedInnermost
56            {
57              int i;
58            public:
59              NestedInnermost() { cout << "ctor of NestedInnermost\n"; }
60              ~NestedInnermost() { cout << "dtor of NestedInnermost\n"; }
61            };
62     
63            NestedInner() { cout << "ctor of NestedInner\n"; }
64            ~NestedInner() { cout << "dtor of NestedInner\n"; }
65          };
66     
67          NestedOuter() { cout << "ctor of NestedOuter\n"; }
68          ~NestedOuter() { cout << "dtor of NestedOuter\n"; }
69        };
70     
71        NestedOutermost() { cout << "ctor of NestedOutermost\n"; }
72        ~NestedOutermost() { cout << "dtor of NestedOutermost\n"; }
73      };
74     
75     
76      int main()
77      {
78        Outermost myOutermost;
79        NestedOutermost myNestedOutermost;
80        cout << "sizeof(myOutermost)=" << sizeof(myOutermost) << endl;
81        cout << "sizeof(myNestedOutermost)=" =<<sizeof(myNestedOutermost)
82          << endl;
83        return 0;
84      }


*****  Output  *****

ctor of Innermost
ctor of Inner
ctor of Outer
ctor of Outermost
ctor of NestedOutermost
sizeof(myOutermost)=16
sizeof(myNestedOutermost)=4
dtor of NestedOutermost
dtor of Outermost
dtor of Outer
dtor of Inner
dtor of Innermost

As you can see from the output, the creation of the (container relationship) Outermost object results in four constructor calls whereas the NestedOutmost results in only one constructor call.

沒有留言:

張貼留言