2

重複の可能性:
C++ コンストラクター名に続くコロンは何をしますか?

CUDA に関する本を読んでいますが、この C++ 構文を読むのに苦労しています。何を検索したらいいのかわからないので、ここに投稿します。

struct cuComplex {
    float   r;
    float   i;
    cuComplex( float a, float b ) : r(a) , i(b)  {}
}

声明は何をしcuComplexますか?具体的には:

cuComplex( float a, float b ) : r(a) , i(b)  {}

それについて学ぶことができるように、これは何と呼ばれていますか?

4

3 に答える 3

6

This is C++ syntax.

cuComplex( float a, float b )

is the constructor defined for this struct.

: r(a) , i(b)

メンバーの初期化と呼ばれます。ここで、ローカルメンバー r および i は、コンストラクターに渡されるパラメーター a および b に設定されます。

残りは空の関数実装です。

于 2011-01-26T08:11:38.633 に答える
1

: r(a) , i(b) cuComplex ctor では、括弧内の値を使用して割り当て時にメモリを構築します。

struct cuComplex {
    const float   r;
    const float   i;
    cuComplex( float a, float b ) : r(a) , i(b)  {} // ok 
}

struct cuComplex {
    const float   r;
    const float   i;
    cuComplex( float a, float b ) {
        r = a;
        i = b;
    } // fail because once allocated, const memory can't be modified
}
于 2011-01-26T10:05:11.870 に答える
1

That is C++, not C, as C structs cannot contain functions in that manner (they could contain a function pointer, but that is irrelevant to the question). That is a constructor for the type "cuComplex" that takes two floats. It initializes the two member variables 'r' and 'r' with the passed in values.

EDIT per comment: The r(a) and i(b) parts are initializing the member variables with the values of the parameters to the constructor.

于 2011-01-26T08:10:24.503 に答える