在comp.lang.c++上看到的一个讨论。关于作用域的,不过后来又涉及到了初始化的问题。
原文:
Hi folks.
Yesterday I modified some old code of mine, and ran into
trouble when I tested it. It turned out that I had renamed a
variable to a name that was already in use in the relevant
scope.Basically, the situation was as in this function:
double f(double a)
{
if(1)
{
double a = a; // <------------- This actually compiles...
}
return a;
}
The variable name was already used in the argument to
the function, and I had inadvertedly used the same name
for a variable inside an if-block. As in the example above,
I used the parameter version of the variable to initialize
the local version.
What surprised me after I found the cause of the bug
was that the compiler actually accepted that piece
of code without complaints. Is that a bug in my compiler
or is the above legal C++ code?
一个比较好的回复:
On Oct 30, 5:30 am, Rune Allnor <all...@tele.ntnu.no> wrote:
> double f(double a)
> {
> if(1)
> {
> double a = a; // <------------- This actually compiles...
> }
> return a;
> }
[...]
> I used the parameter version of the variable to initialize
> the local version.
Unfortunately that's not true. You used the uninitialized local
version to initialize itself. :)
> What surprised me after I found the cause of the bug
> was that the compiler actually accepted that piece
> of code without complaints. Is that a bug in my compiler
> or is the above legal C++ code?
It is legal; as always, the inner name hides the outer name. There is
only one 'a' during that initialization. The following construct is
valid on its own:
void f()
{
double a = a;
}
You have an uninitialized 'a' at hand.
Ali
具体文章:http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/50c264587143056e/655a9dc8c7344ded#655a9dc8c7344ded
作用域问题经这个老兄解释就明白了。但是初始化赋值问题有点疑惑。
自己验证了一下代码如下:
//classT.h
#include <iostream>
class classT
{
public:
classT(int v=0)
{
val=v;
}
classT(classT& t)
{
val=t.val;
}
void getv()
{
std::cout<<val<<std::endl;
}
private:
int val;
};
//main.cpp
#include "classT.h"
int main(int argc, char* argv[])
{
classT t1=t1;
classT t2;
std::cout<<"t1:/t";
t1.getv();
std::cout<<"t2:/t";
t2.getv();
return 0;
}
结果:
t1: -858993460
t2: 0
可见讨论中的那位老兄分析正确.
至于其中原因,还没弄清.
暂记于此.
原文:
Hi folks.
Yesterday I modified some old code of mine, and ran into
trouble when I tested it. It turned out that I had renamed a
variable to a name that was already in use in the relevant
scope.Basically, the situation was as in this function:
double f(double a)
{
if(1)
{
double a = a; // <------------- This actually compiles...
}
return a;
}
The variable name was already used in the argument to
the function, and I had inadvertedly used the same name
for a variable inside an if-block. As in the example above,
I used the parameter version of the variable to initialize
the local version.
What surprised me after I found the cause of the bug
was that the compiler actually accepted that piece
of code without complaints. Is that a bug in my compiler
or is the above legal C++ code?
一个比较好的回复:
On Oct 30, 5:30 am, Rune Allnor <all...@tele.ntnu.no> wrote:
> double f(double a)
> {
> if(1)
> {
> double a = a; // <------------- This actually compiles...
> }
> return a;
> }
[...]
> I used the parameter version of the variable to initialize
> the local version.
Unfortunately that's not true. You used the uninitialized local
version to initialize itself. :)
> What surprised me after I found the cause of the bug
> was that the compiler actually accepted that piece
> of code without complaints. Is that a bug in my compiler
> or is the above legal C++ code?
It is legal; as always, the inner name hides the outer name. There is
only one 'a' during that initialization. The following construct is
valid on its own:
void f()
{
double a = a;
}
You have an uninitialized 'a' at hand.
Ali
具体文章:http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/50c264587143056e/655a9dc8c7344ded#655a9dc8c7344ded
作用域问题经这个老兄解释就明白了。但是初始化赋值问题有点疑惑。
自己验证了一下代码如下:
//classT.h
#include <iostream>
class classT
{
public:
classT(int v=0)
{
val=v;
}
classT(classT& t)
{
val=t.val;
}
void getv()
{
std::cout<<val<<std::endl;
}
private:
int val;
};
//main.cpp
#include "classT.h"
int main(int argc, char* argv[])
{
classT t1=t1;
classT t2;
std::cout<<"t1:/t";
t1.getv();
std::cout<<"t2:/t";
t2.getv();
return 0;
}
结果:
t1: -858993460
t2: 0
可见讨论中的那位老兄分析正确.
至于其中原因,还没弄清.
暂记于此.