5

Here's the statement. I believe this is using a cast operator, but what's the deal with the post increment?

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

Declaration and definition of C:

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

Declaration of the conditional_density class:

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];
  }

};

The parent class, datmoConditionalDensity, only has a virtual destructor.

It would have been easy to answer this by debugging the code, but this code won't build under Windows (needs a bunch of external libraries).

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];
  }

它返回对双精度的引用。最后:

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

因为重载()运算符返回对双精度的引用,所以我可以使用++它来增加双精度。

于 2012-09-18T20:14:04.293 回答
3

如果我正确地解释了这一点,C则它是指向函数对象(已operator()定义的东西)的指针。这意味着

(*C)(x_i,gi_tn,f)

表示“取消引用C以取回函数对象,然后使用参数x_igi_tn和调用它f。请注意,函数返回 a 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 回答