我们可以覆盖[] []吗? [英] Can we override [][] ?

查看:56
本文介绍了我们可以覆盖[] []吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为[] []执行操作符覆盖,但无法找出

语法。我试过这个(没有编译的代码// //


类CMyBitmap

{

public:

CMyBitmap(int Rows,int Columns):Rows_(Rows),Columns_(Columns)

{

Data_ = new SPixel [Rows *专栏];

}

~CMyBitmap()

{

delete [] Data_;

}


//错误C2804:二进制''运算符[''参数太多

// SPixel& operator [](const int Row,const int Column)

// {

//返回Data_ [Columns_ * Row + Column];

//}


//错误C2092:''[]''数组元素类型不能是函数

// SPixel& operator [] [](const int Row, const int Column)


SPixel& Pixel(const int Row,const int Column)

{

返回Data_ [Columns_ *行+列];

}


私人:

SPixel * Data_;

int Rows_;

int Columns_;


//没有副本或作业,所以禁用私有。

CMyBitmap(CMyBitmap const& ){};

CMyBitmap& operator =(CMyBitmap const&){};


};


我们可以覆盖2d数组访问吗?

I wanted to do an operator override for [][] but couldnt'' figure out the
syntax. I tried this (code that doesn''t compile commented out with //:

class CMyBitmap
{
public:
CMyBitmap( int Rows, int Columns ): Rows_( Rows ), Columns_( Columns )
{
Data_ = new SPixel[ Rows * Columns ];
}
~CMyBitmap()
{
delete[] Data_;
}

// error C2804: binary ''operator ['' has too many parameters
// SPixel& operator[]( const int Row, const int Column )
// {
// return Data_[ Columns_ * Row + Column ];
// }

// error C2092: ''[]'' array element type cannot be function
// SPixel& operator[][]( const int Row, const int Column )

SPixel& Pixel( const int Row, const int Column )
{
return Data_[ Columns_ * Row + Column ];
}

private:
SPixel* Data_;
int Rows_;
int Columns_;

// No copy or assignment yet so disable by making private.
CMyBitmap ( CMyBitmap const& ) {};
CMyBitmap& operator=( CMyBitmap const& ) {};

};

Can we override 2d array access?

推荐答案



" Jim Langston" < TA ******* @ rocketmail.com>在消息中写道

新闻:KB *************** @ fe04.lga ...

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:KB***************@fe04.lga...
我想做一个算子覆盖[] [],但无法弄清楚
语法。我尝试了这个(没有编译的代码// //

类CMyBitmap
公开:
CMyBitmap(int Rows,int Columns) :Rows_(Rows),Columns_(Columns)
{
Data_ = new SPixel [Rows * Columns];
}
~CMyBitmap()
{
删除[] Data_;
}
//错误C2804:二进制''运算符[''参数太多
// SPixel& operator [](const int Row ,const int Column)
// {
//返回Data_ [Columns_ * Row + Column];
//}

//错误C2092:'' []''数组元素类型不能是函数
// SPixel& operator [] [](const int Row,const int Column)

SPixel& Pixel(const int Row,const int专栏)
{
返回Data_ [Columns_ * Row + Column];
}
私人:
SPixel * Data_;
int Rows_ ;
int Columns_;

//没有副本或赋值然后通过私密禁用。
CMyBitmap(CMyBitmap const& ){};
CMyBitmap& operator =(CMyBitmap const&){};

};

我们可以覆盖2d数组访问吗?
I wanted to do an operator override for [][] but couldnt'' figure out the
syntax. I tried this (code that doesn''t compile commented out with //:

class CMyBitmap
{
public:
CMyBitmap( int Rows, int Columns ): Rows_( Rows ), Columns_( Columns )
{
Data_ = new SPixel[ Rows * Columns ];
}
~CMyBitmap()
{
delete[] Data_;
}

// error C2804: binary ''operator ['' has too many parameters
// SPixel& operator[]( const int Row, const int Column )
// {
// return Data_[ Columns_ * Row + Column ];
// }

// error C2092: ''[]'' array element type cannot be function
// SPixel& operator[][]( const int Row, const int Column )

SPixel& Pixel( const int Row, const int Column )
{
return Data_[ Columns_ * Row + Column ];
}

private:
SPixel* Data_;
int Rows_;
int Columns_;

// No copy or assignment yet so disable by making private.
CMyBitmap ( CMyBitmap const& ) {};
CMyBitmap& operator=( CMyBitmap const& ) {};

};

Can we override 2d array access?




当然可以。但是......

[] []不是运算符,它是单个

运算符[]的两个实例。如果你有你的算子[]做''

'',你就不会有任何问题使用表达式

如x [],x [ ] [],x [] [] [],等等。如果你需要回复
更多的帮助或一个例子。


-Mike



Certainly. But...
[][] is not an operator, it''s two instances of the single
operator []. If you have your operator[] do the ''right
thing'', you shouldn''t have any problems using expressions
such as x[], x[][], x[][][], etc. Post back if you need
more help or an example.

-Mike




" Mike Wahler" < MK ****** @ mkwahler.net>在消息中写道

新闻:5I **************** @ newsread2.news.pas.earthli nk.net ...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:5I****************@newsread2.news.pas.earthli nk.net...

Jim Langston < TA ******* @ rocketmail.com>在消息中写道
新闻:KB *************** @ fe04.lga ...

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:KB***************@fe04.lga...
我想为[]做一个操作符覆盖[]但无法弄清楚
语法。我尝试了这个(没有编译的代码// //

类CMyBitmap
公开:
CMyBitmap(int Rows,int Columns) :Rows_(Rows),Columns_(Columns)
{
Data_ = new SPixel [Rows * Columns];
}
~CMyBitmap()
{
删除[] Data_;
}
//错误C2804:二进制''运算符[''参数太多
// SPixel& operator [](const int Row ,const int Column)
// {
//返回Data_ [Columns_ * Row + Column];
//}

//错误C2092:'' []''数组元素类型不能是函数
// SPixel& operator [] [](const int Row,const int Column)

SPixel& Pixel(const int Row,const int专栏)
{
返回Data_ [Columns_ * Row + Column];
}
私人:
SPixel * Data_;
int Rows_ ;
int Columns_;

//没有副本或赋值然后通过私密禁用。
CMyBitmap(CMyBitmap const& ){};
CMyBitmap& operator =(CMyBitmap const&){};

};

我们可以覆盖二维数组访问吗?
I wanted to do an operator override for [][] but couldnt'' figure out the
syntax. I tried this (code that doesn''t compile commented out with //:

class CMyBitmap
{
public:
CMyBitmap( int Rows, int Columns ): Rows_( Rows ), Columns_( Columns )
{
Data_ = new SPixel[ Rows * Columns ];
}
~CMyBitmap()
{
delete[] Data_;
}

// error C2804: binary ''operator ['' has too many parameters
// SPixel& operator[]( const int Row, const int Column )
// {
// return Data_[ Columns_ * Row + Column ];
// }

// error C2092: ''[]'' array element type cannot be function
// SPixel& operator[][]( const int Row, const int Column )

SPixel& Pixel( const int Row, const int Column )
{
return Data_[ Columns_ * Row + Column ];
}

private:
SPixel* Data_;
int Rows_;
int Columns_;

// No copy or assignment yet so disable by making private.
CMyBitmap ( CMyBitmap const& ) {};
CMyBitmap& operator=( CMyBitmap const& ) {};

};

Can we override 2d array access?



当然可以。但是......
[] []不是运算符,它是单个
运算符[]的两个实例。如果你有你的算子[]做正确的事情,你就不应该使用表达式等问题,例如x [],x [] [],x [] [ ] []等等。如果你需要更多的帮助或示例,请回复。



Certainly. But...
[][] is not an operator, it''s two instances of the single
operator []. If you have your operator[] do the ''right
thing'', you shouldn''t have any problems using expressions
such as x[], x[][], x[][][], etc. Post back if you need
more help or an example.




我为操作员[]护目镜但是护目镜没有似乎索引特殊的

字符(例如''['')因此命中率不是很有序而且我花了很长时间才能找到它[]我的手册的语法没有给我任何

的例子,是时候得到一本新的手册。


我需要一个例子,如同我不知道如何覆盖[]接受

两个参数,但你似乎表明它是[]的两个实例。什么是

''正确的事情''或者你能指导我到这个主题的网页吗?正如我所说,在这种情况下,谷歌不是我的朋友。



I had goggled for operator[] but goggle doesn''t seem to index special
characters (such as ''['') so the hits weren''t very orderly and it took me a
long time to even find the syntax for [] My manual didn''t give me any
examples, time to get a new manual.

I would need an example please, as I don''t know how to override[] to accept
two paramters but you seem to indicate it is two instances of []. What is
the ''right thing'' or can you direct me to a web page on the subject? As I
said, in this case Google was not my friend.


Jim Langston发布:

Jim Langston posted:

我需要一个例子,因为我不知道如何覆盖[]来接受两个参数,但你似乎表明它是
[]的两个实例。什么是'正确的''或者你能指导我到这个主题的网页吗?正如我所说,在这种情况下,谷歌不是我的朋友。
I would need an example please, as I don''t know how to override[] to
accept two paramters but you seem to indicate it is two instances of
[]. What is the ''right thing'' or can you direct me to a web page on
the subject? As I said, in this case Google was not my friend.



我只花了十五分钟,所以这绝不是完美的。

未经测试的代码:

class ChessBoard {

public:


class Square {

public:

enum SquareContents {

空,典当,城堡,马,主教,女王,国王}内容;


Square& operator =(SquareContents const sc)

{

contents = sc;

return * this;

}

};

方形正方形[64];

类Horizo​​ntalCoordinate {

私人:


ChessBoard& cb;

unsigned const x;


public:


Horizo​​ntalCoordinate(ChessBoard& arg_cb,unsigned const arg_x)

:cb(arg_cb),x(arg_x){}


Square&运算符[](无符号常数)

{

返回cb.squares [x * 8 + y];

}

};

Horizo​​ntalCoordinate operator [](unsigned const x)

{

返回Horizo​​ntalCoordinate(* this,x);

}


};


int main()

{

ChessBoard board;


board [3] [5] = ChessBoard :: Square :: bishop;

}

如你所见,一个简单的成员功能(甚至功能风格

运营商)可能会更好。


-Tomás


I only spent fifteen minutes on this, so it''s by no means perfect.

Untested code:
class ChessBoard {
public:

class Square {
public:
enum SquareContents {
empty, pawn, castle, horse, bishop, queen, king } contents;

Square &operator=( SquareContents const sc )
{
contents = sc;
return *this;
}
};
Square squares[64];
class HorizontalCoordinate {
private:

ChessBoard &cb;
unsigned const x;

public:

HorizontalCoordinate(ChessBoard &arg_cb, unsigned const arg_x)
: cb(arg_cb), x(arg_x) {}

Square &operator[]( unsigned const y )
{
return cb.squares[ x * 8 + y ];
}
};
HorizontalCoordinate operator[](unsigned const x)
{
return HorizontalCoordinate(*this, x);
}

};

int main()
{
ChessBoard board;

board[3][5] = ChessBoard::Square::bishop;
}
As you can see, a simple member function (or even function-style
operator) would probably be better.

-Tomás


这篇关于我们可以覆盖[] []吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆