[英]overloading [][] operators in c++
我正在用c ++編寫矩陣3x3類。
glm :: mat3通過[][] operator
語法提供對矩陣數據的訪問。
例如myMatrix[0][0] = 1.0f;
將第一行,第一列輸入設置為1.0f 。
我想提供類似的訪問權限。 如何重載[][] operator
?
我嘗試了以下內容,但是我收到了錯誤:
必須將運算符名稱聲明為函數
const real operator[][](int row, int col) const
{
// should really throw an exception for out of bounds indices
return ((row >= 0 && row <= 2) && (col >= 0 && col <= 2)) ? _data[row][col] : 0.0f;
}
重載此運算符的正確方法是什么?
沒有operator [][]
,所以你需要重載[]
運算符兩次:一次在矩陣上,為行返回一個代理對象,為返回的代理行返回一次:
// Matrix's operator[]
const row_proxy operator[](int row) const
{
return row_proxy(this, row);
}
// Proxy's operator[]
const real operator[](int col) const
{
// Proxy stores a pointer to matrix and the row passed into the first [] operator
return ((this->row >= 0 && this->row <= 2) && (col >= 0 && col <= 2)) ? this->matrix->_data[this->row][col] : 0.0f;
}
使方法double operator() (int row, int col) const
更容易。 而不是matrix[i][j]
你只是說matrix(i,j)
。
通常,對於多個參數,您要使用operator()
,而不是operator[]
。
嗯,如果不是很明顯,C ++中沒有operator[][]
。 這只是operator[]
應用了兩次。 這意味着如果你想要那個符號,那么你必須讓第一個返回第二個可以應用的結果(可索引的東西或代理)。
下面的代碼草擬了一些方法,選擇你喜歡的:
#include <iostream>
#include <vector>
template< int n >
int& dummy() { static int elem = n; return elem; }
struct Mat1
{
int operator() ( int const x, int const y ) const
{ return dummy<1>(); }
int& operator() ( int const x, int const y )
{ return dummy<1>(); }
Mat1( int, int ) {}
};
struct Mat2
{
int at( int const x, int const y ) const
{ return dummy<2>(); }
int& at( int const x, int const y )
{ return dummy<2>(); }
Mat2( int, int ) {}
};
struct Mat3
{
struct At { At( int x, int y ) {} };
int operator[]( At const i ) const
{ return dummy<3>(); }
int& operator[]( At const i )
{ return dummy<3>(); }
Mat3( int, int ) {}
};
class Mat4
{
protected:
int get( int const x, int const y ) const
{ return dummy<4>(); }
void set( int const x, int const y, int const v ) {}
class AssignmentProxy
{
private:
Mat4* pMat_;
int x_;
int y_;
public:
void operator=( int const v ) const
{ pMat_->set( x_, y_, v ); }
int value() const { return pMat_->get( x_, y_ ); }
operator int () const { return value(); }
AssignmentProxy( Mat4& mat, int const x, int const y )
: pMat_( &mat ), x_( x ), y_( y )
{}
};
public:
int operator()( int const x, int const y ) const
{ return get( x, y ); }
AssignmentProxy operator()( int const x, int const y )
{ return AssignmentProxy( *this, x, y ); }
Mat4( int, int ) {}
};
class Mat5
{
protected:
int at( int const x, int const y ) const
{ return dummy<4>(); }
int& at( int const x, int const y )
{ return dummy<5>(); }
class RowReadAccess
{
private:
Mat5 const* pMat_;
int y_;
public:
int operator[]( int const x ) const
{
return pMat_->at( x, y_ );
}
RowReadAccess( Mat5 const& m, int const y )
: pMat_( &m ), y_( y )
{}
};
class RowRWAccess
{
private:
Mat5* pMat_;
int y_;
public:
int operator[]( int const x ) const
{
return pMat_->at( x, y_ );
}
int& operator[]( int const x )
{
return pMat_->at( x, y_ );
}
RowRWAccess( Mat5& m, int const y )
: pMat_( &m ), y_( y )
{}
};
public:
RowReadAccess operator[]( int const y ) const
{ return RowReadAccess( *this, y ); }
RowRWAccess operator[]( int const y )
{ return RowRWAccess( *this, y ); }
Mat5( int, int ) {}
};
struct Mat6
{
private:
std::vector<int> elems_;
int width_;
int height_;
int indexFor( int const x, int const y ) const
{
return y*width_ + x;
}
public:
int const* operator[]( int const y ) const
{
return &elems_[indexFor( 0, y )];
}
int* operator[]( int const y )
{
return &elems_[indexFor( 0, y )];
}
Mat6( int const w, int const h )
: elems_( w*h, 6 ), width_( w ), height_( h )
{}
};
int main()
{
using namespace std;
enum{ w = 1024, h = 1024 };
typedef Mat3::At At;
Mat1 m1( w, h );
Mat2 m2( w, h );
Mat3 m3( w, h );
Mat4 m4( w, h );
Mat5 m5( w, h );
Mat6 m6( w, h );
wcout
<< m1( 100, 200 ) // No fuss simple, but exposes element ref.
<< m2.at( 100, 200 ) // For those who don't like operators.
<< m3[At( 100, 200)] // If you really want square brackets mnemonic.
<< m4( 100, 200 ) // Hides element ref by using assignment proxy.
<< m5[200][100] // Ditto but with square brackets (more complex).
<< m6[200][100] // The minimum fuss square brackets, exposes elem ref.
<< endl;
}
哦,我在發布代碼后發現我還沒有完全隱藏Mat5
的內部存儲:它需要額外的代理級別,如Mat4
。 所以這種方法非常復雜。 我不會這樣做(我認為Mat1
很好很容易),但有些人認為代理很酷,數據隱藏得更酷......
總結一下,沒有“正確”的方法來重載operator[]
。 有很多方法(如上面的代碼所示),每種方式都有一些權衡。 通常你最好使用operator()
,因為與operator[]
相反,它可以使用任意數量的參數。
沒有[][]
運算符。 GLM的方式是從第一個[]
返回vec3&
。 vec3
有自己的[]
運算符重載。 所以它是兩個單獨的類上的兩個獨立的operator[]
。
這也是GLSL的工作原理。 第一個[]
將列作為向量。 第二個采用向量並從中獲取值。
表達式foo[1][2]
實際上被解釋為(foo[1])[2]
,即[]
運算符從變量foo
開始連續應用兩次。 沒有[][]
運算符被重載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.