为什么数组索引在此程序中不显示交换行为?
Why array indexing not showing commutative behavior in this program?
我知道数组索引在 C 和 C++ 中都是可交换的,所以 a[i] 与 i[a] 相同,它与 i[a] 具有相同的效果,并且两者都是有效的。但是最近我写了一个以下程序,当我使用 i[intob] 而不是 intob[i] 时编译失败。
#include <iostream>
#include <cstdlib>
using std::cout;
template<class T>class atype
{
T* a;
int size;
public:
atype(int n)
{
size=n;
a=new T[size];
for(auto i=0;i<size;i++)
a[i]=i;
}
~atype()
{
delete[] a;
}
T& operator[](int i);
};
template<class T> T& atype<T>::operator[](int i)
{
if(i<0 || i>size)
{
cout<<"\nIndex value of ";
cout<<i<<" is out of bounds.\n";
exit(1);
}
return a[i]; // i[a] also works fine here.
}
int main()
{
int i,n;
cout<<"Enter value of n: ";
std::cin>>n;
atype<int> intob{n};
atype<long int> longintob{n};
cout<<"Integer array: ";
for(i=0;i<n;i++)
intob[i]=i;
for(i=0;i<n;i++)
cout<<i[intob]<<' '; // oops compiler error why???
cout<<'\n';
cout<<"long integer array: ";
for(i=0;i<n;i++)
longintob[i]=i;
for(i=0;i<n;i++)
cout<<longintob[i]<<' ';
longintob[15]=123;
}
我遇到以下编译器错误。
[错误] 'operator[]' 不匹配(操作数类型为 'int' 和 'atype')
但是如果我在重载的 [] 运算符函数中编写 i[a] 那么它就可以正常工作。为什么?
是否存在使用 i[intob] 访问数组元素的解决方案?
如果我哪里错了或者理解有误,请告诉我。
索引运算符本身是不可交换的。
如果您了解数组(或指针)a
和索引 i
那么 a[i]
等同于 *(a + i)
,这将有所帮助。交换位来自该加法,因为 *(a + i)
等于 *(i + a)
,这导致 i[a]
有效。
因为编译器会检查类型,而在 C++ 中有一种叫做运算符重载的东西,这意味着我可以为自己的类型编写自己的运算符:
typedef int stupidInt;
intoperator+(int l, stupidInt r){
return l-r;
}
int operator+(stupidInt l, int r){
return l+r;
}
stupidInt a = 3;
int b = 7;
a + b == 10;
b + a == 4;
这也适用于 operator[](typea, typeb);
某处可能有一个定义 operator[](atype, int)
。 (也许这是在编译器中硬编码的)
但反之则不然。
也没有什么理由需要将其反过来。但你可能会定义
int operator[](int i, atype a){
return a[i];
}
这应该使运算符也可以反向工作。
我知道数组索引在 C 和 C++ 中都是可交换的,所以 a[i] 与 i[a] 相同,它与 i[a] 具有相同的效果,并且两者都是有效的。但是最近我写了一个以下程序,当我使用 i[intob] 而不是 intob[i] 时编译失败。
#include <iostream>
#include <cstdlib>
using std::cout;
template<class T>class atype
{
T* a;
int size;
public:
atype(int n)
{
size=n;
a=new T[size];
for(auto i=0;i<size;i++)
a[i]=i;
}
~atype()
{
delete[] a;
}
T& operator[](int i);
};
template<class T> T& atype<T>::operator[](int i)
{
if(i<0 || i>size)
{
cout<<"\nIndex value of ";
cout<<i<<" is out of bounds.\n";
exit(1);
}
return a[i]; // i[a] also works fine here.
}
int main()
{
int i,n;
cout<<"Enter value of n: ";
std::cin>>n;
atype<int> intob{n};
atype<long int> longintob{n};
cout<<"Integer array: ";
for(i=0;i<n;i++)
intob[i]=i;
for(i=0;i<n;i++)
cout<<i[intob]<<' '; // oops compiler error why???
cout<<'\n';
cout<<"long integer array: ";
for(i=0;i<n;i++)
longintob[i]=i;
for(i=0;i<n;i++)
cout<<longintob[i]<<' ';
longintob[15]=123;
}
我遇到以下编译器错误。
[错误] 'operator[]' 不匹配(操作数类型为 'int' 和 'atype')
但是如果我在重载的 [] 运算符函数中编写 i[a] 那么它就可以正常工作。为什么?
是否存在使用 i[intob] 访问数组元素的解决方案?
如果我哪里错了或者理解有误,请告诉我。
索引运算符本身是不可交换的。
如果您了解数组(或指针)a
和索引 i
那么 a[i]
等同于 *(a + i)
,这将有所帮助。交换位来自该加法,因为 *(a + i)
等于 *(i + a)
,这导致 i[a]
有效。
因为编译器会检查类型,而在 C++ 中有一种叫做运算符重载的东西,这意味着我可以为自己的类型编写自己的运算符:
typedef int stupidInt;
intoperator+(int l, stupidInt r){
return l-r;
}
int operator+(stupidInt l, int r){
return l+r;
}
stupidInt a = 3;
int b = 7;
a + b == 10;
b + a == 4;
这也适用于 operator[](typea, typeb);
某处可能有一个定义 operator[](atype, int)
。 (也许这是在编译器中硬编码的)
但反之则不然。
也没有什么理由需要将其反过来。但你可能会定义
int operator[](int i, atype a){
return a[i];
}
这应该使运算符也可以反向工作。