“隐藏构造函数”的作用是什么?编译 C++ 时的警告意思是与 g++?
使用以下代码:
#include <stdio.h>
struct my_struct {
int a;
int b;
my_struct();
};
my_struct::my_struct(void)
{
printf("constructor\n");
}
void my_struct(void)
{
printf("standard function\n");
}
int main (int argc, char *argv[])
{
struct my_struct s;
s.a = 1;
s.b = 2;
printf("%d-%d\n", s.a, s.b);
return 0;
}
我在使用 g++ -Wshadow main.cpp 进行编译时收到一条警告:
main.cpp:15:20: warning: ‘void my_struct()’ hides constructor for ‘struct my_struct’
如果 void my_struct 函数实际上替换了 my_struct::my_struct 函数,我会接受该警告。但事实似乎并非如此。如果我运行该程序,我会得到:
constructor
1-2
知道这个警告是什么意思吗?这很烦人,尤其是当我将 C 头文件包含到 C++ 代码中时
Using the following code:
#include <stdio.h>
struct my_struct {
int a;
int b;
my_struct();
};
my_struct::my_struct(void)
{
printf("constructor\n");
}
void my_struct(void)
{
printf("standard function\n");
}
int main (int argc, char *argv[])
{
struct my_struct s;
s.a = 1;
s.b = 2;
printf("%d-%d\n", s.a, s.b);
return 0;
}
I get a warning compiling with g++ -Wshadow main.cpp:
main.cpp:15:20: warning: ‘void my_struct()’ hides constructor for ‘struct my_struct’
I would be ok with that warning if the void my_struct function actually replaced the my_struct::my_struct one. But it does not appears to be the case. If I run the program, I get:
constructor
1-2
Any idea what this warning mean ? It is quite annoying especially when I include C headers into C++ code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该警告指出
my_struct()
函数与my_struct
结构同名。这意味着您将无法编写:因为编译器会认为您正在使用函数作为类型。但是,正如您可能意识到的那样,您仍然可以使用
struct
关键字实例化您的结构:The warning points out that the
my_struct()
function has the same name as themy_struct
structure. It means you will be unable to write:Because the compiler will think that you're using a function as a type. However, as you probably realized, you can still instantiate your structure with the
struct
keyword:void my_struct(void)
与您的类/结构具有相同的名称,并且由于它位于全局命名空间中,因此它与您的类/结构的构造函数发生冲突。您可以尝试类似的操作:
顺便说一句,
struct my_struct s;
中的struct
在 C++ 中是多余的。void my_struct(void)
has the same name of your class/struct and since it is in the global namespace it is conflicting with your class/struct's constructor.You could try something like:
And by the way the
struct
instruct my_struct s;
is redundant in C++.这意味着有时 GNU 编译器套件发出的警告有点偏差。 (尝试省略
struct my_struct
定义中右大括号后面的分号。如果您使用的不是最新版本的 g++,则错误消息将会有点偏差。)这里隐藏了一些东西,但它不是 struct my_struct 的构造函数。被隐藏的是作为类型标识符的名称
my_struct
。如果从变量s
的声明中删除struct
,您可以看到这一点:使用my_struct s;
而不是struct my_struct s;
如果没有struct
关键字提供的上下文信息,编译器现在必须将my_struct
解释为函数名称。It means that sometimes the warnings issued by the GNU compiler suite are a bit off. (Try omitting the semicolon after the close brace on the definition of
struct my_struct
. If you are using anything but a very recent version of g++ the error message will be a bit off.)Something is being hidden here, but it is not the constructor for
struct my_struct
. What is being hidden is the namemy_struct
as a type identifier. You can see this in action if you remove thestruct
from the declaration of the variables
: Usemy_struct s;
instead ofstruct my_struct s;
Without the contextual information offered by thestruct
keyword, the compiler now must interpretmy_struct
as a function name.