5

これがステートメントです。これはキャスト演算子を使用していると思いますが、ポストインクリメントはどうなりますか?

(*C)(x_i,gi_tn,f)++;

の宣言と定義C

std::auto_ptr<conditional_density> C(new conditional_density());

conditional_densityクラスの宣言:

class conditional_density: public datmoConditionalDensity{
public:
  static const double l_min, l_max, delta;
  static double x_scale[X_COUNT];    // input log luminance scale
  double *g_scale;    // contrast scale
  double *f_scale;    // frequency scale      
  const double g_max;    
  double total;    
  int x_count, g_count, f_count; // Number of elements    
  double *C;          // Conditional probability function    
  conditional_density( const float pix_per_deg = 30.f ) :
    g_max( 0.7f ){
    //Irrelevant to the question               
  }    

  double& operator()( int x, int g, int f )
  {
    assert( (x + g*x_count + f*x_count*g_count >= 0) && (x + g*x_count + f*x_count*g_count < x_count*g_count*f_count) );
    return C[x + g*x_count + f*x_count*g_count];
  }

};

親クラスdatmoConditionalDensityは、仮想デストラクタのみを持っています。

コードをデバッグすることでこれに答えるのは簡単でしたが、このコードはWindowsではビルドされません(多数の外部ライブラリが必要です)。

4

3 に答える 3

11
(*C)(x_i,gi_tn,f)++;

それを分解しましょう:

(*C)

これにより、ポインタが逆参照されます。Cはスマートポインタであるため、参照を解除して、ポイントされている実際の要素を取得できます。結果はconditional_densityオブジェクトです。

(*C)(x_i,gi_tn,f)

()これにより、クラス内のオーバーロードされた演算子が呼び出されconditional_densityます。初めて見るのは不思議かもしれませんが、他のすべてと同じようにオペレーターです。要するに、このコードを呼び出すということです。

  double& operator()( int x, int g, int f )
  {
    assert( (x + g*x_count + f*x_count*g_count >= 0) && (x + g*x_count + f*x_count*g_count < x_count*g_count*f_count) );
    return C[x + g*x_count + f*x_count*g_count];
  }

これは、doubleへの参照を返します。ついに:

(*C)(x_i,gi_tn,f)++

オーバーロード()された演算子はdoubleへの参照を返すため、double++をインクリメントする演算子を使用できます。

于 2012-09-18T20:14:04.293 に答える
3

これを正しく解釈している場合Cは、関数オブジェクト(operator()定義されているもの)へのポインターです。この意味は

(*C)(x_i,gi_tn,f)

「逆参照Cして関数オブジェクトを取得し、引数、、、およびを使用して呼び出すことを意味しますx_igi_tn関数fはを返すことに注意してください。double&したがって、この行は

(*C)(x_i,gi_tn,f)++;

「Cを逆参照し、適切な引数を使用して関数を呼び出し、最後に結果をポストインクリメントする」ことを意味します。

お役に立てれば!

于 2012-09-18T20:12:58.573 に答える
0

operator()(int、int、int)は、doubleの静的配列の要素への参照を返します。

++演算子は、返された値をインクリメントします。

于 2012-09-18T20:13:23.280 に答える