language: C++11 (gcc-4.7.2)
date: 181 days 3 hours ago
link:
visibility: public
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
#include <iostream>
 
    struct Cloneable
        {
        protected:
                Cloneable(){}
                Cloneable(const Cloneable &){}
        public:
                virtual ~Cloneable(){}
 
                Cloneable &operator=(const Cloneable &) = delete;
 
                virtual Cloneable *Clone() const = 0;
                static void ReleaseClone(Cloneable *&clone)
                {
                        clone->ReleaseClone(), clone = /*nullptr*/0;
                }
        protected:
                virtual void ReleaseClone() = 0;
        public:
//              struct CloningException : public virtual Exception
//              {
//                      CloningException(){}
//                      CloningException(const char *msg) : Exception(msg) {}
//                      CloningException(const CloningException &) = delete;
//                      CloningException &operator=(const CloningException &) = delete;
//                      virtual ~CloningException(){}
//              };
 
                template<typename InheritingClass>
                struct Auto : public virtual Cloneable
                {
                        typedef InheritingClass ICT;
                        virtual ICT *Clone() const
                        {
                                const ICT *t = dynamic_cast<const ICT *>(this);
                                if(!t)
                                {
//                                      throw CloningException();
                                }
                                return new ICT(*t);
                        }
                protected:
                        virtual void ReleaseClone()
                        {
                                if(!dynamic_cast<const ICT *>(this))
                                {
//                                      throw CloningException();
                                }
                                delete this;
                        }
                public:
                        virtual ~Auto(){}
                };
        };
 
struct MyAbstractClass : public virtual Cloneable
{
    MyAbstractClass(){}
protected:
    MyAbstractClass(const MyAbstractClass &){}
public:
    virtual ~MyAbstractClass(){}
    
    MyAbstractClass &operator=(const MyAbstractClass &) = delete;
 
    virtual void Tick() = 0;
};
 
struct MyImplementationClass : public virtual MyAbstractClass, public virtual Cloneable::Auto<MyImplementationClass>
{
    MyImplementationClass(){}
protected:
    MyImplementationClass(const MyImplementationClass &){}
public:
    virtual ~MyImplementationClass(){}
    
    MyImplementationClass &operator=(const MyImplementationClass &) = delete;
    
    virtual void Tick();
    
    using Cloneable::Auto<MyImplementationClass>::Clone;
protected:
    using Cloneable::Auto<MyImplementationClass>::ReleaseClone;
};
 
void MyImplementationClass::Tick()
{
    std::cout << "Tick" << std::endl;
}
 
int main()
{
    MyImplementationClass a;
    MyAbstractClass *b = a.Clone();
    b->Tick();
    Cloneable::ReleaseClone(b);
}
 
prog.cpp:32:3: error: invalid use of incomplete type 'struct Cloneable'
prog.cpp:4:2: error: forward declaration of 'struct Cloneable'
prog.cpp: In function 'int main()':
prog.cpp:94:27: error: cannot declare variable 'a' to be of abstract type 'MyImplementationClass'
prog.cpp:71:1: note:   because the following virtual functions are pure within 'MyImplementationClass':
prog.cpp:13:22: note: 	virtual Cloneable* Cloneable::Clone() const
prog.cpp:19:16: note: 	virtual void Cloneable::ReleaseClone()
prog.cpp:97:30: error: no matching function for call to 'Cloneable::ReleaseClone(MyAbstractClass*&)'
prog.cpp:14:15: note: candidates are: static void Cloneable::ReleaseClone(Cloneable*&)
prog.cpp:19:16: note:                 virtual void Cloneable::ReleaseClone()
prog.cpp: In member function 'ICT* Cloneable::Auto<InheritingClass>::Clone() const [with InheritingClass = MyImplementationClass, ICT = MyImplementationClass]':
prog.cpp:95:34:   instantiated from here
prog.cpp:41:22: error: cannot allocate an object of abstract type 'MyImplementationClass'
prog.cpp:71:1: note:   since type 'MyImplementationClass' has pure virtual functions