Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this C++ syntax mean?

Tags:

c++

syntax

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).

like image 660
dario_ramos Avatar asked Sep 18 '12 20:09

dario_ramos


1 Answers

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

Let's break it down:

(*C)

This dereferences the pointer. C is a smart pointer, and thus can be dereferenced to get the actual element being pointed to. The result is a conditional_density object.

(*C)(x_i,gi_tn,f)

This calls the the overloaded () operator in the conditional_density class. It can be strange to see it the first time, but it is an operator just like everything else. Bottom line is that it calls this code:

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

which returns a reference to a double. Finally:

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

Because the overloaded () operator returns a reference to a double, I can use ++ on it which increments the double.

like image 135
riwalk Avatar answered Oct 07 '22 05:10

riwalk