Why can I define structures and classes within a function in C++?

后端 未结 6 1591
日久生厌
日久生厌 2020-12-04 07:03

I just mistakenly did something like this in C++, and it works. Why can I do this?

int main(int argc, char** argv) {
    struct MyStruct
    {
      int some         


        
6条回答
  •  日久生厌
    2020-12-04 07:46

    Well, basically, why not? A struct in C (going back to the dawn of time) was just a way to declare a record structure. If you want one, why not be able to declare it where you would declare a simple variable?

    Once you do that, then remember that a goal of C++ was to be compatible with C if at all possible. So it stayed.

提交回复
热议问题