: 난 그냥 내 코드에서 버그가 수 있도록 보인다,뿐만 아니라 그것을 컴파일 무엇을 발견
In C arrays why is this true? a[5] == 5[a]
Accessing arrays by index[array] in C and C++불가능 C++ 배열 인덱싱 가능한 중복
, 그것은 또한 작동 예상대로 처음에 ...
다음 코드 스 니펫을 고려하십시오.
#include <string>
#include <iostream>
using namespace std;
class WeirdTest
{
public:
int value;
string text;
WeirdTest() : value(0),
text("")
{}
virtual ~WeirdTest()
{}
void doWeirdTest()
{
value = 5;
string name[] =
{
"Zero",
"One",
"Two",
"Three",
"Four",
"Five"
};
text = value[name];
cout << "text: " << text << endl;
}
};
int main(int argc, char** argv)
{
WeirdTest test;
test.doWeirdTest();
return 0;
}
text=value[name];
대신에 "text=name[value];
"이어야하지만 컴파일러는 불평하지 않으며 결과 바이너리 코드는 "버그"가 있는지 여부와 완전히 동일합니다. g ++ 4.6.3을 사용하여 컴파일 중이며, 누군가 여기서 무슨 일이 일어나고 있는지 알고 있으면 매우 감사 할 것입니다. 표준에서 내가 놓친 뭔가 일 수 있습니까? C++ 0x 자동 버그 수정? ;)
고마워,
건배!
C에서 상속 된 동작입니다. 참조 : http://www.c-faq.com/aryptr/joke.html – jamesdlin
참조 http://stackoverflow.com/questions/8969684/accessing-array-contents-xn -vs-nx/8969755 # 8969755 또는 http://stackoverflow.com/questions/5073350/accessing-arrays-by-indexarray-in-c-and-c – hmjd