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

前端 未结 3 451
误落风尘
误落风尘 2021-01-21 06:11

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 fi         


        
相关标签:
3条回答
  • 2021-01-21 06:45

    For the most part, a C++ file is parsed top-to-bottom, so entities must be declared before they are used.

    In your class, bar2 and b are invalid because they both make use of my_int_type, which has not yet been declared.

    One exception to the "top-to-bottom" parsing rule is member functions that are defined inside the definition of their class. When such a member function definition is parsed, it is parsed as if it appeared after the definition of the class. This is why your usage of my_int_type in bar is valid.

    Effectively, this:

    struct foo
    {
        void bar()
        {
            my_int_type b;
        }
    
        typedef int my_int_type;
    };
    

    is the same as:

    struct foo
    {
        void bar();
    
        typedef int my_int_type;
    };
    
    inline void foo::bar()
    {
        my_int_type b;
    }
    
    0 讨论(0)
  • 2021-01-21 06:49

    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.

    0 讨论(0)
  • 2021-01-21 06:50

    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.

    0 讨论(0)
提交回复
热议问题