に似てシンプルかつ効率的である:
void needs_x_only(Foo const& object)
{
int const x = object.x(); // Using single item getter function.
(void) x;
}
x()
機能は、コンパイラはおそらくこれをダウン最適化するinline
ある場合関連するデータメンバーの直接アクセス。
out-argumentsを持つルーチンとして設計された複数項目ゲッターを使用すると、すべての項目を必要としない場合はあまり明確でなくなり、引数としてconst
を使用できなくなります。 const
(一般的には望ましい)をサポートする1つの方法は、小さなラッパー関数を書くことです。例えば、ここ
auto position_of(Foo const& object)
-> Point<int>
{
int x;
int y;
object.get_xy(x, y); // Using out-arguments getter routine.
return {x, y};
}
Point
は、2D点struct
を定義するいくつかのクラスです。
が続いラッパーの使用は、次のようになります。
void needs_both_x_and_y(Foo const& object)
{
Point<int> const position = position_of(object);
(void) position;
}
あなたはまだ何がここで質問です、というん機能を書きましたか? C++で関数を呼び出す方法は? – Lundin