File: virtconsorg.cc

package info (click to toggle)
c%2B%2B-annotations 7.2.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 11,484 kB
  • ctags: 2,902
  • sloc: cpp: 15,844; makefile: 2,997; ansic: 165; perl: 90; sh: 29
file content (144 lines) | stat: -rw-r--r-- 2,687 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <vector>
#include <typeinfo>

using namespace std;

// waarom werkt clone() in de CC niet? Omdat bij de vector's
// push_back(Derived1()) de CC wordt aangeroepen, en niet other.clone(),
// terwijl 't niet duidelijk is hoe
// je in de CC clone() kunt gebruiken. *this = *other->clone() werkt
// niet.

class Base
{
    public:
        Base()
        {
            cout << "Base() " << ++s_n << "\n";
        }
        virtual ~Base()
        {
            std::cout << "~Base() << s_n--\n";
        }
        Base(Base const &other)
        {
            std::cout << "Base(Base const &)\n";
        }

        virtual Base *clone() const
        {
            std::cout << "Base clone()\n";
            return new Base();
        }
};

class Derived1: public Base
{
    public:
        Derived1()
        {
            std::cout << "Derived1()\n";
        }
        Derived1(Derived1 const &other)
        {
            std::cout << "Derived1(Derived1)\n";
        }
        ~Derived1()
        {
            std::cout << "~Derived1()\n";
        }
        virtual Base *clone() const
        {
            std::cout << "Derived1::clone()\n";
            return new Derived1(*this);
        }
};

class BWrap
{
    Base *d_bp;

    public:
        BWrap()
        :
            d_bp(new Base())
        {}
        BWrap(Base const &bp)
        :
            d_bp(bp.clone())
        {}
        BWrap(Base const *bp)
        :
            d_bp(bp->clone())
        {}
        ~BWrap()
        {
            destroy();
        }
        BWrap(BWrap const &other)
        {
            copy(other);
        }
        BWrap &operator=(BWrap const &other)
        {
            if (this != &other)
            {
                destroy();
                copy(other);
            }
            return *this;
        }

        Base &base() const
        {
            return *d_bp;
        }
    private:
        void destroy()
        {
            delete d_bp;
        }
        void copy(BWrap const &other)
        {
            d_bp = other.d_bp->clone();
        }
};

int main()
{
    vector<Base> bv(1);
    cout << "==\n";

    Derived1 d1;
    cout << "==\n";

    bv.push_back(d1);
    cout << "==\n";
/*
    vector<BWrap> vb;
    cout << "==\n";

    vb.push_back(Derived1());
    cout << "==\n";

    vector<BWrap> vb2(vb);
    cout << "==\n";

/ *
    Derived1 d1;
    vb.push_back(d1);
    cout << "==\n";

    Derived1 dp = &vb[0].ref();
    cout << dp << endl;
    cout << "==\n";

    vb[0] = Base();
    cout << "==\n";

    cout << typeid(vb[0].base()).name() << endl;
    cout << typeid(vb[1].base()).name() << endl;
    cout << "==\n";
*/
}