Note that, C++, unlike C, which doesn't have a notion for constructors, supports object construction and destruction, so if you were to define a variable of a class type in a for loop, like the following,class MyClass { public: MyClass() { cout << "hello world" << endl; }};//...for (int i = 0; i < 10; ++i) { MyClass m;}您每次都会调用其构造函数,有效地打印"hello world"十次.这与C声明有很大不同,不应与C声明混淆.you'd call its constructor every time, effectively printing "hello world" ten times. This is very different from C declarations and should not be confused with it. 这篇关于在循环内声明的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 19:10