File: non_copyable_classes.hpp

package info (click to toggle)
pygccxml 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,444 kB
  • sloc: xml: 29,841; python: 13,914; cpp: 2,671; makefile: 163; ansic: 59
file content (130 lines) | stat: -rw-r--r-- 2,628 bytes parent folder | download | duplicates (5)
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
// Copyright 2014-2017 Insight Software Consortium.
// Copyright 2004-2009 Roman Yakovenko.
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt

// A non copyable class, see:
// http://stackoverflow.com/questions/2173746/how-do-i-make-this-c-object-non-copyable

#ifndef __non_copyable_classes_hpp__
#define __non_copyable_classes_hpp__

namespace non_copyable{

// A first base class, which can not be copied
class Foo1 {
  private:
    Foo1();
    Foo1( const Foo1& ); // non construction-copyable
    Foo1& operator=( const Foo1& ); // non copyable
  protected:
    int var;
  public:
    void set_values (int a)
      { var=a; }
};

class MainFoo1: public Foo1 {
  public:
    int get_var()
      { return var; }
};

// -----------------------------------------------

// Foo2 is a base class, with a non copiable const
// The constant is of type fundamental (int)
class Foo2 {
  private:
    Foo2();
  protected:
    const int var;
};

// Use the base class
class MainFoo2: public Foo2 {
  public:
    int get_var()
      { return var; }
};

// -----------------------------------------------

// A class which does nothing
class Something
{
public:
    int m_nValue;
};

// Foo3 is a base class, with a non copiable const
// The constant is a class
// See http://www.learncpp.com/cpp-tutorial/810-const-class-objects-and-member-functions/
class Foo3 {
  private:
    Foo3();
  protected:
    const Something cSomething; // calls default constructor
};

// Use the base class
class MainFoo3 : Foo3 {
  public:
    char b;
};

// -----------------------------------------------

// Foo4 is a base class, with a non copiable const
// The constant is an array
class Foo4 {
  private:
    Foo4();
  protected:
    const int foo [5];
};

// Use the base class
class MainFoo4 : Foo4 {
  public:
    char b;
};

// -----------------------------------------------

// Foo5 is a base class, with a variable foo of type Foo4, which is not
// copyable
class Foo5 {
  private:
    Foo5();
  protected:
    const Foo4 foo;
};

// Use the base class
class MainFoo5 : Foo5 {
  public:
    char b;
};

// ----------------------------------------------- Member with static qualifiers

// Foo6 is a base class (with a static const variable foo)
class Foo6 {
  private:
    Foo6();
  protected:
    static const int foo1;
};

// Use the base class: this class is copyable, it has a public ctor, and
// the base class does not contain non-copyable members (because of the static
// qualifier)
class MainFoo6 : Foo6 {
  public:
    MainFoo6();
};

}

#endif//__non_copyable_classes_hpp__