c++ - 这个 C++ 语法是什么意思?

标签 c++ syntax

声明如下。我相信这是在使用强制转换运算符,但是后增量有什么用呢?

(*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 下构建(需要一堆外部库)。

最佳答案

(*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。

关于c++ - 这个 C++ 语法是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12484222/

相关文章:

c++ - 如何创建一个对象创建函数,该函数将由与其关联的名称调用?

带有 block /proc/lambda 的 Ruby 双管道分配?

c - 我的 else..if 声明有错误

c++ - 未定义对 boost::system::generic_category() 的引用,尽管库已提供给 g++

c++ - 无法在 visual studio 2008 中打开 qt 项目文件

c++ - 让 ostream 打开

python - 为什么在初始化期间这是python语法错误?

c++ - 为什么按值传递而不是按常量引用传递?

java - Dynamodb java 语法

sql-server - sql语句中方括号[]有什么用?