初始化问题 [英] initialization problem
问题描述
请考虑以下代码snipet:
void f(const std :: string& msg)
{
// ...
for(...)
{
// .. 。
std :: string msg = msg +" " + another_string; // 1
// ...
}
// ...
}
这段代码是懒惰搜索/替换的结果(s / msg0 / msg / g -
最初的函数参数和_ = _后的第一个变量
是msg0)。
但编译器是否应该抱怨初始化
对象使用相同的对象?我错过了什么?
TIA,
Marcelo Pinto
Hi,
Consider the following code snipet:
void f(const std::string & msg)
{
//...
for (...)
{
//...
std::string msg = msg + " " + another_string; //1
//...
}
//...
}
This code is the consequence of lazy Search/Replace (s/msg0/msg/g -
initially the function parameter and the first variable after the _=_
was msg0).
But shouldn''t the compiler complain about the initialization of an
object use the same object? What am I missing?
TIA,
Marcelo Pinto
推荐答案
Marcelo Pinto写道:
Marcelo Pinto wrote:
请考虑以下代码snipet:
void f(const std :: string& msg)
{
// ...
for(...)
//
。 ..
std :: string msg = msg +" " + another_string; // 1
// ...
}
// ...
}
Hi,
Consider the following code snipet:
void f(const std::string & msg)
{
//...
for (...)
{
//...
std::string msg = msg + " " + another_string; //1
//...
}
//...
}
但不应该是编译器抱怨初始化
But shouldn''t the compiler complain about the initialization of an
对象使用相同的对象?我缺少什么?
object use the same object? What am I missing?
哪个编译器?上面的代码(在删除所有额外部分后)给出了Comeau在线重新声明
重新声明错误(msg已经在当前范围内宣布为
)
>
Marcelo Pinto写道:
Marcelo Pinto wrote:
请考虑以下代码snipet:
void f (const std :: string& msg)
{
// ...
for(...)
{
//...
std :: string msg = msg +" " + another_string; // 1
// ...
}
// ...
这段代码是懒惰搜索/替换的结果( s / msg0 / msg / g -
最初函数参数和_ = _
之后的第一个变量是msg0)。
但编译器不应该抱怨初始化对象使用相同的对象?我缺少什么?
Hi,
Consider the following code snipet:
void f(const std::string & msg)
{
//...
for (...)
{
//...
std::string msg = msg + " " + another_string; //1
//...
}
//...
}
This code is the consequence of lazy Search/Replace (s/msg0/msg/g -
initially the function parameter and the first variable after the _=_
was msg0).
But shouldn''t the compiler complain about the initialization of an
object use the same object? What am I missing?
您可以在不同的
范围内重新声明已经可见的标识符。只重新定义已在
中定义的标识符,相同的范围是非法的并产生编译器错误。
你的for()循环体是一个新的范围,因此标识符msg可以再次定义为
。你必须要注意,虽然你有效地隐藏了已经可见的标识符,这可能不是你想要做的b
。
- peter
you are allowed to redeclare already visible identifiers in different
scopes. only redefining an identifier that has already been defined in
the same scope is illegal and yields a compiler error.
your for() loop body is a new scope, thus the identifier msg can be
defined again. you have to be aware though that you are effectively
hiding the already visible identifier, which is probably not what you
intended to do.
-- peter
Neelesh Bodas写道:
Neelesh Bodas wrote:
Marcelo Pinto写道:
Marcelo Pinto wrote:
请考虑以下代码snipet:
void f(const std :: string& msg)
/
/ / ...
for(...)
{
// ...
std :: string msg = msg +" " + another_string; // 1
// ...
//
}
Hi,
Consider the following code snipet:
void f(const std::string & msg)
{
//...
for (...)
{
//...
std::string msg = msg + " " + another_string; //1
//...
}
//...
}
但不应该'编译器抱怨初始化
But shouldn''t the compiler complain about the initialization of an
对象使用相同的对象?我缺少什么?
object use the same object? What am I missing?
哪个编译器?上面的代码(在删除所有额外部分后)给出了Comeau在线重新声明的重新声明错误(msg已经在当前范围内声明了
)
Which compiler? The above code (after removing all extra portion) gives
redeclaration error with Comeau online (msg has already been declared
in the current scope)
真的吗?你也删除了''for''吗?它为内部引入了适当的
范围限制。 msg ...
V
Really? Did you remove the ''for'' as well? It''s introduces the proper
scope limitation for the "inner" msg...
V
这篇关于初始化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!