我用 C++ 编程已经有一段时间了,直到今天我才想到这个。
考虑以下代码:
struct foo
{
// compiles fine
void bar()
{
a = 1;
my_int_type b;
b = 5;
}
// Just a declaration, this fails to compile, which leads me to assume that
// even though the bar() method is declared and defined all at once, the
// compiler looks/checks-syntax-of the class interface first, and then compiles
// the respective definitions...?
void bar2(my_int_type); // COMPILE ERROR
my_int_type b; // COMPILE ERROR because it comes before the typedef declaration
typedef int my_int_type;
my_int_type a;
void bar3(my_int_type); // compiles fine
};
int main()
{
foo a;
a.bar();
return 0;
}
我对错误发生原因的理解(见bar2()
上面的评论)是否正确/不正确?无论哪种方式,我都希望能简单地概述一下单程 C++ 编译器如何编译上面给出的代码。