回答編集履歴

4

見当外れだったようなので削除

2018/01/18 09:43

投稿

colonq
colonq

スコア88

test CHANGED
@@ -1,153 +1 @@
1
- ポインタのサイズは一定なのでC++14ならばvectorのunique_ptrでメンバー変数を持てばいいかと。
2
-
3
- 一応書いてはみましたが、初心者には少々難しすぎるかもしれません。
4
-
5
- ```C++
6
-
7
- #include <iostream>
8
-
9
- #include <vector>
10
-
11
- #include <memory>
12
-
13
- #include <utility>
14
-
15
-
16
-
17
- class Value
18
-
19
- {
20
-
21
- public:
22
-
23
- Value() = default;
24
-
25
- Value(const Value& v) : number(v.number), values(nullptr)
26
-
27
- {
28
-
29
- if (v.values) values = std::make_unique<std::vector<Value>>(*(v.values));
30
-
31
- }
32
-
33
- Value(Value&&) = default;
34
-
35
- Value& operator =(const Value& v)
36
-
37
- {
38
-
39
- if (this != &v) {
40
-
41
- number = v.number;
42
-
43
- values = nullptr;
44
-
45
- if (v.values) values = std::make_unique<std::vector<Value>>(*(v.values));
46
-
47
- }
48
-
49
- return *this;
50
-
51
- }
52
-
53
- Value& operator =(Value&&) = default;
54
-
55
- ~Value() = default;
56
-
57
-
58
-
59
- Value(double n) : number(n) {}
60
-
61
- Value& operator =(double n) {number = n;}
62
-
63
- Value(const std::vector<Value>&);
64
-
65
- void print() const;
66
-
67
-
68
-
69
- private:
70
-
71
- double number;
72
-
73
- std::unique_ptr<std::vector<Value>> values;
74
-
75
- };
76
-
77
-
78
-
79
- Value::Value(const std::vector<Value>& v)
80
-
81
- : number(0.0)
82
-
83
- , values(std::make_unique<std::vector<Value>>(v))
84
-
85
- {}
86
-
87
-
88
-
89
- void Value::Value::print() const
90
-
91
- {
92
-
93
- if (values) {
94
-
95
- auto& v = *values;
96
-
97
- int size = static_cast<int>(v.size());
98
-
99
- std::cout << "{";
100
-
101
- v[0].print();
102
-
103
- for (int i = 1; i < size; ++i) {
104
-
105
- std::cout << ",";
106
-
107
- v[i].print();
108
-
109
- }
110
-
111
- std::cout << "}";
112
-
113
- } else {
114
-
115
- std::cout << number;
116
-
117
- }
118
-
119
- }
120
-
121
-
122
-
123
- int main()
124
-
125
- {
126
-
127
- std::vector<Value> v1 = {3.4, 5.6};
128
-
129
- Value v2 = 7.8;
130
-
131
- std::vector<Value> v3 = {1.2, v1, v2};
132
-
133
- Value v4 = v3;
134
-
135
-
136
-
137
- v4.print();
138
-
139
- std::cout << std::endl;
140
-
141
-
142
-
143
- using V = std::vector<Value>;
144
-
145
- Value v5 = V{0.1, V{0.3, 0.5}, 0.7};
1
+ 再帰呼び出しが全く無関係とは。まるで見当外れだったようなので削除します。
146
-
147
- v5.print();
148
-
149
- std::cout << std::endl;
150
-
151
- }
152
-
153
- ```

3

valuesの初期化も忘れてた:-)

2018/01/18 09:43

投稿

colonq
colonq

スコア88

test CHANGED
@@ -39,6 +39,8 @@
39
39
  if (this != &v) {
40
40
 
41
41
  number = v.number;
42
+
43
+ values = nullptr;
42
44
 
43
45
  if (v.values) values = std::make_unique<std::vector<Value>>(*(v.values));
44
46
 

2

number = v.number の位置を移動

2018/01/17 15:05

投稿

colonq
colonq

スコア88

test CHANGED
@@ -36,9 +36,9 @@
36
36
 
37
37
  {
38
38
 
39
- number = v.number;
39
+ if (this != &v) {
40
40
 
41
- if (this != &v) {
41
+ number = v.number;
42
42
 
43
43
  if (v.values) values = std::make_unique<std::vector<Value>>(*(v.values));
44
44
 

1

numberの初期化を忘れていたので修正

2018/01/17 13:38

投稿

colonq
colonq

スコア88

test CHANGED
@@ -35,6 +35,8 @@
35
35
  Value& operator =(const Value& v)
36
36
 
37
37
  {
38
+
39
+ number = v.number;
38
40
 
39
41
  if (this != &v) {
40
42
 
@@ -134,8 +136,16 @@
134
136
 
135
137
  std::cout << std::endl;
136
138
 
139
+
140
+
141
+ using V = std::vector<Value>;
142
+
143
+ Value v5 = V{0.1, V{0.3, 0.5}, 0.7};
144
+
145
+ v5.print();
146
+
147
+ std::cout << std::endl;
148
+
137
149
  }
138
150
 
139
-
140
-
141
151
  ```