在 for 循环中重新声明对象 - C++
Re-declaring object in a for loop - C++
我对循环中的变量重新声明有疑问。
为什么在 foor 循环中声明对象不会触发重新声明错误?
对象是否在循环的每次迭代中被销毁并重新创建?
我正在插入示例代码
class DataBlock {
int id;
string data;
public:
DataBlock(int tid=0,const string &tdata=""){
id=tid;
data=tdata;
}
}
int main(int argc, char *argv[]){
ifstream file;
int temp_id; //temporary hold the the id read from the file
string temp_data; //temporary hold the data read from the file
set <DataBlock> s;
//check for command line input here
file.open(argv[1]);
//check for file open here
file >> temp_id >> temp_data;
while (!file.eof()){
DataBlock x(temp_id,temp_data); //Legit, But how's the workflow?
s.insert(x);
file >> temp_id >> temp_data;
}
file.close();
return 0;
}
Why declaring an object in a foor loop doesn't trigger the redeclaration error?
不,不是。
每次 for 循环迭代时,都会进入一个新的作用域,并销毁在前一个作用域中创建的对象并释放它们的存储分配。
for (int i=0 ; i<2 ; ++i) {
MyClass c;
}
就好像:
{
int i=0;
{
MyClass c; // c(0)
} // c destructed, storage allocation freed
++i;
{
MyClass c; // c(1)
} // c destructed, storage allocation freed
++i;
}
c(0)
和 c(1)
确实共享相同的名称,但在范围内没有重叠。一切都很好。
Why declaring an object in a foor loop doesn't trigger the redeclaration error?
当您在同一范围内两次(或多次)声明相同的名称时,会发生重新声明错误。看起来像
int i = 5;
int i = 6; // uh oh, you already declared i
在你的循环中你没有那个,你只有
loop
{
int i = 5;
}
所以没有重新声明。
你也可以
int i = 5
{
int i = 6;
std::cout << i;
}
并且不会出现重新声明错误,因为变量在不同的范围内,您可以在多个范围内使用相同的变量。我在这种情况下会打印 6
,因为 i
是范围内的 i
。
Do the object get destroyed and recreated at each iteration of the loop?
是的。将循环视为被多次调用的函数。当您进入 loop/function 的主体时,其中声明的变量将被构造 1 并且当您到达 loop/function 的末尾时,变量将被销毁。
1:它有点复杂,但我们不需要在这个答案中深入了解所有这些细节
我对循环中的变量重新声明有疑问。
为什么在 foor 循环中声明对象不会触发重新声明错误?
对象是否在循环的每次迭代中被销毁并重新创建?
我正在插入示例代码
class DataBlock {
int id;
string data;
public:
DataBlock(int tid=0,const string &tdata=""){
id=tid;
data=tdata;
}
}
int main(int argc, char *argv[]){
ifstream file;
int temp_id; //temporary hold the the id read from the file
string temp_data; //temporary hold the data read from the file
set <DataBlock> s;
//check for command line input here
file.open(argv[1]);
//check for file open here
file >> temp_id >> temp_data;
while (!file.eof()){
DataBlock x(temp_id,temp_data); //Legit, But how's the workflow?
s.insert(x);
file >> temp_id >> temp_data;
}
file.close();
return 0;
}
Why declaring an object in a foor loop doesn't trigger the redeclaration error?
不,不是。
每次 for 循环迭代时,都会进入一个新的作用域,并销毁在前一个作用域中创建的对象并释放它们的存储分配。
for (int i=0 ; i<2 ; ++i) {
MyClass c;
}
就好像:
{
int i=0;
{
MyClass c; // c(0)
} // c destructed, storage allocation freed
++i;
{
MyClass c; // c(1)
} // c destructed, storage allocation freed
++i;
}
c(0)
和 c(1)
确实共享相同的名称,但在范围内没有重叠。一切都很好。
Why declaring an object in a foor loop doesn't trigger the redeclaration error?
当您在同一范围内两次(或多次)声明相同的名称时,会发生重新声明错误。看起来像
int i = 5;
int i = 6; // uh oh, you already declared i
在你的循环中你没有那个,你只有
loop
{
int i = 5;
}
所以没有重新声明。
你也可以
int i = 5
{
int i = 6;
std::cout << i;
}
并且不会出现重新声明错误,因为变量在不同的范围内,您可以在多个范围内使用相同的变量。我在这种情况下会打印 6
,因为 i
是范围内的 i
。
Do the object get destroyed and recreated at each iteration of the loop?
是的。将循环视为被多次调用的函数。当您进入 loop/function 的主体时,其中声明的变量将被构造 1 并且当您到达 loop/function 的末尾时,变量将被销毁。
1:它有点复杂,但我们不需要在这个答案中深入了解所有这些细节