如何输入和输出方括号运算符[]
how to input and output of square bracket operator []
我不知道如何重载方括号运算符“[]”,它既可以输入也可以输出,这意味着我可以:
_class ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;
我看到了 this question,它给出了这个代码:
unsigned long operator [](int i) const {return registers[i];}
unsigned long & operator [](int i) {return registers[i];}
这对我不起作用:-(我试过并这样做了:
struct coord {
int x;
int y;
};
class _map
{
public:
struct coord c{3,4};
char operator[](struct coord) const // this is supposed to suppor output
{
cout << "this1" << endl;
return 'x';
}
char& operator[](struct coord) // this is supposed to support input
{
cout << "this2" << endl;
return c.x;
}
void operator= (char enter)
{
cout << enter;
}
};
然后我主要做了:
_map ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;
这给了我:
this2
this2
这意味着我无法创建两个 diff 函数来创建两个 diff 功能,例如:
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;
******************编辑前*************************** *
我正在尝试将方形运算符 [] 覆盖到输入和输出信息。而方括号的输入是一个结构。
我尝试这个灵感来自 this question:
struct coord {
int x;
int y;
};
class _map
{
public:
char operator[](struct coord) const // this is supposed to suppor output
{
cout << "this1" << endl;
return 'x';
}
char& operator[](struct coord) // this is supposed to support input
{
cout << "this2" << endl;
char a = 'a';
return a;
}
void operator= (char enter)
{
cout << enter;
}
};
然后我主要做了:
_map ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;
这给了我:
this2
this2
将输入运算符的输入更改为 int 时一切正常:
char operator[](int coord) const
{
cout << "this1" << endl;
return 'x';
}
然后我主要做了:
_map ppp;
ppp[{1,2}] = 1;
char x = ppp[2] ;
然后我得到:
this2
this1
这是我的H.W。但我只是问一些不是硬件主要部分的东西,而且我正在研究这个小东西一段时间...
答案全靠HolyBlackCat!
方括号 ("[]") 的覆盖函数将 return 像这样的引用:
char& operator[](coord c)
{
return board[c.x][c.y];
}
因此我们可以为它分配一个字符,因为它是对某个内存槽的引用,如下所示:
_map ppp;
ppp[{1,2}] = 1;
另一方面,我们将能够检索其中的内容,因为引用指向如下字符:
char x = ppp[{1,2}] ;
这意味着不需要像以前认为的那样有两个重写函数。
我不知道如何重载方括号运算符“[]”,它既可以输入也可以输出,这意味着我可以:
_class ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;
我看到了 this question,它给出了这个代码:
unsigned long operator [](int i) const {return registers[i];}
unsigned long & operator [](int i) {return registers[i];}
这对我不起作用:-(我试过并这样做了:
struct coord {
int x;
int y;
};
class _map
{
public:
struct coord c{3,4};
char operator[](struct coord) const // this is supposed to suppor output
{
cout << "this1" << endl;
return 'x';
}
char& operator[](struct coord) // this is supposed to support input
{
cout << "this2" << endl;
return c.x;
}
void operator= (char enter)
{
cout << enter;
}
};
然后我主要做了:
_map ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;
这给了我:
this2
this2
这意味着我无法创建两个 diff 函数来创建两个 diff 功能,例如:
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;
******************编辑前*************************** *
我正在尝试将方形运算符 [] 覆盖到输入和输出信息。而方括号的输入是一个结构。
我尝试这个灵感来自 this question:
struct coord {
int x;
int y;
};
class _map
{
public:
char operator[](struct coord) const // this is supposed to suppor output
{
cout << "this1" << endl;
return 'x';
}
char& operator[](struct coord) // this is supposed to support input
{
cout << "this2" << endl;
char a = 'a';
return a;
}
void operator= (char enter)
{
cout << enter;
}
};
然后我主要做了:
_map ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;
这给了我:
this2
this2
将输入运算符的输入更改为 int 时一切正常:
char operator[](int coord) const
{
cout << "this1" << endl;
return 'x';
}
然后我主要做了:
_map ppp;
ppp[{1,2}] = 1;
char x = ppp[2] ;
然后我得到:
this2
this1
这是我的H.W。但我只是问一些不是硬件主要部分的东西,而且我正在研究这个小东西一段时间...
答案全靠HolyBlackCat!
方括号 ("[]") 的覆盖函数将 return 像这样的引用:
char& operator[](coord c)
{
return board[c.x][c.y];
}
因此我们可以为它分配一个字符,因为它是对某个内存槽的引用,如下所示:
_map ppp;
ppp[{1,2}] = 1;
另一方面,我们将能够检索其中的内容,因为引用指向如下字符:
char x = ppp[{1,2}] ;
这意味着不需要像以前认为的那样有两个重写函数。