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

后端 未结 6 1584
日久生厌
日久生厌 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:59

    It's mentioned at, for example, section "7.8: Local classes: classes inside functions" of http://www.icce.rug.nl/documents/cplusplus/cplusplus07.html which calls it a "local class" and says it "can be very useful in advanced applications involving inheritance or templates".

提交回复
热议问题