'Why doesn't the order of methods in a class matter in C++?

I have been programming in C++ for quite some time and I never thought about this until today.

Consider the following code:

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;
}

Is my understanding of why the errors occur (see bar2() comment above) correct/incorrect? Either way, I would appreciate an answer with a simplistic overview of how a single-pass C++ compiler would compile the code given above.



Solution 1:[1]

Compiler just starts to go down in a block. Any symbol which is not familiar to it will be considered as a new symbol which is not defined. This is the scheme behind the function definition or header files.

You can suppose that the compiler first makes a list of definitions so the bar() method should get compiled correctly because the definitions have provided before.

Solution 2:[2]

It has a lot to do with visibility. I think your confusion may come from assuming a single pass. Think of class parsing as done in two stages.

  1. Parse class definition.
  2. Parse method implementation.

The advantage to this is we have visibility of the entire class from within class member functions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kamran Amini
Solution 2