teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

5

追記

2020/08/06 22:26

投稿

退会済みユーザー
answer CHANGED
@@ -127,4 +127,14 @@
127
127
  }
128
128
 
129
129
  ```
130
- 見当違いな回答でしたら申し訳ございません。
130
+ 見当違いな回答でしたら申し訳ございません。
131
+
132
+ 追記
133
+ ---
134
+ friendはオブジェクト指向の三大要素の内の一つ
135
+ カプセル化に反する機能です。
136
+ やむを得ない事情がない限り
137
+ 好き好んで使わないほうが良いかと思われます。
138
+ もし頭の中にfriendが浮かんだのなら
139
+ ゲッターで解決できないか考えてみては
140
+ いかがでしょうか。

4

誤記修正

2020/08/06 22:26

投稿

退会済みユーザー
answer CHANGED
@@ -46,7 +46,7 @@
46
46
  static constexpr unsigned int Z = 2;
47
47
 
48
48
  public:
49
- XYZ(const double x, const double y, const double z):x_(x_),y_(y),z_(z){}
49
+ XYZ(const double x, const double y, const double z):x_(x),y_(y),z_(z){}
50
50
 
51
51
  void add(const double x, const double y, const double z){
52
52
  this->x_ += x;

3

細かな点修正

2020/08/06 22:05

投稿

退会済みユーザー
answer CHANGED
@@ -36,26 +36,27 @@
36
36
  //
37
37
  //////////////
38
38
 
39
- //XYZクラスで使用する定数
40
- constexpr unsigned int X = 0;
41
- constexpr unsigned int Y = 1;
42
- constexpr unsigned int Z = 2;
43
-
44
39
  class XYZ{
45
- double x, y, z;
40
+ double x_, y_, z_;
46
41
 
47
42
  public:
43
+ //XYZクラスで使用する定数
48
- XYZ(const double x_arg, const double y_arg, const double z_arg):x(x_arg),y(y_arg),z(z_arg){}
44
+ static constexpr unsigned int X = 0;
45
+ static constexpr unsigned int Y = 1;
46
+ static constexpr unsigned int Z = 2;
49
47
 
48
+ public:
49
+ XYZ(const double x, const double y, const double z):x_(x_),y_(y),z_(z){}
50
+
50
- void add(const double x_arg, const double y_arg, const double z_arg){
51
+ void add(const double x, const double y, const double z){
51
- this->x += x_arg;
52
+ this->x_ += x;
52
- this->y += y_arg;
53
+ this->y_ += y;
53
- this->z += z_arg;
54
+ this->z_ += z;
54
55
  }
55
56
 
56
57
  double& getRef(const unsigned int index){
57
58
  assert(index<=Z);// 今回のように定数で処理を切り替えたいのならassertで入力制限設けたほうが良いかと思われます。
58
- return index==X ? this->x : index==Y ? this->y : this->z;
59
+ return index==X ? this->x_ : index==Y ? this->y_ : this->z_;
59
60
  }
60
61
  };
61
62
 
@@ -67,14 +68,14 @@
67
68
  XYZ& f5(XYZ& xyz){return xyz;}
68
69
 
69
70
  XYZ& f6(XYZ& xyz){
70
- xyz.getRef(X) += 1;
71
+ xyz.getRef(XYZ::X) += 1;
71
- xyz.getRef(Y) += 1;
72
+ xyz.getRef(XYZ::Y) += 1;
72
- xyz.getRef(Z) += 1;
73
+ xyz.getRef(XYZ::Z) += 1;
73
74
  return xyz;
74
75
  }
75
76
 
76
- double sum(XYZ& xyz){return xyz.getRef(X) + xyz.getRef(Y) + xyz.getRef(Z);}// 注意点:XYZにはメンバ変数の参照を返すゲッターがあるのでfriend化不要
77
+ double sum(XYZ& xyz){return xyz.getRef(XYZ::X) + xyz.getRef(XYZ::Y) + xyz.getRef(XYZ::Z);}// 注意点:XYZにはメンバ変数の参照を返すゲッターがあるのでfriend化不要
77
- double f7(XYZ& xyz){return xyz.getRef(X) * xyz.getRef(Y) * xyz.getRef(Z);}
78
+ double f7(XYZ& xyz){return xyz.getRef(XYZ::X) * xyz.getRef(XYZ::Y) * xyz.getRef(XYZ::Z);}
78
79
 
79
80
  ////////////////
80
81
  //
@@ -109,22 +110,21 @@
109
110
  xyz.add(1,1,1);
110
111
  f5(xyz).add(7, 8, 9); /* equivalent to xyz.add(7, 8, 9);*/
111
112
 
112
- std::cout << xyz.getRef(X) << "\n";
113
+ std::cout << xyz.getRef(XYZ::X) << "\n";
113
- std::cout << xyz.getRef(Y) << "\n";
114
+ std::cout << xyz.getRef(XYZ::Y) << "\n";
114
- std::cout << xyz.getRef(Z) << "\n";
115
+ std::cout << xyz.getRef(XYZ::Z) << "\n";
115
116
 
116
- xyz.getRef(X) = 1;
117
+ xyz.getRef(XYZ::X) = 1;
117
- xyz.getRef(Y) = 2;
118
+ xyz.getRef(XYZ::Y) = 2;
118
- xyz.getRef(Z) = 3;
119
+ xyz.getRef(XYZ::Z) = 3;
119
120
  f6(xyz);
120
121
  std::cout << sum(xyz) << "\n"; // (2)
121
122
  f6(f6(f6(xyz)));
122
- std::cout << xyz.getRef(X) + xyz.getRef(Y) + xyz.getRef(Z) << "\n"; // (3), sum(xyz) と同値である.
123
+ std::cout << xyz.getRef(XYZ::X) + xyz.getRef(XYZ::Y) + xyz.getRef(XYZ::Z) << "\n"; // (3), sum(xyz) と同値である.
123
124
  std::cout << f7(xyz) << "\n";
124
125
 
125
126
  return 0;
126
127
  }
127
128
 
128
-
129
129
  ```
130
130
  見当違いな回答でしたら申し訳ございません。

2

変数名に不備あり

2020/08/06 22:02

投稿

退会済みユーザー
answer CHANGED
@@ -53,9 +53,9 @@
53
53
  this->z += z_arg;
54
54
  }
55
55
 
56
- double& getRef(const unsigned int i){
56
+ double& getRef(const unsigned int index){
57
- assert(i<=Z);// 今回のように定数で処理を切り替えたいのならassertで入力制限設けたほうが良いかと思われます。
57
+ assert(index<=Z);// 今回のように定数で処理を切り替えたいのならassertで入力制限設けたほうが良いかと思われます。
58
- return i==X ? this->x : i==Y ? this->y : this->z;
58
+ return index==X ? this->x : index==Y ? this->y : this->z;
59
59
  }
60
60
  };
61
61
 

1

誤字

2020/08/06 21:56

投稿

退会済みユーザー
answer CHANGED
@@ -6,7 +6,7 @@
6
6
  - f6にreturnが無い。
7
7
  - sum関数をfriend化している。(ゲッターがあるのでその必要なし)
8
8
  - using namespace std;を使っている。
9
- - メンバにアクセスする際(*this).メンバという方法をっている。(this->メンバ でよい)
9
+ - メンバにアクセスする際(*this).メンバという方法をっている。(this->メンバ でよい)
10
10
  - 引数の変数名が分かりづらい。
11
11
  - ゲッターに入力制限を設けていない。
12
12