禁用有关在派生类的复制构造函数内显式初始化基构造函数的警告

发布于 2024-10-13 11:04:08 字数 383 浏览 1 评论 0原文

我正在使用启用了 -Wextra 的 g++ 版本 4.2.1。我包含来自库的标头,并且我不断收到有关库中某个类的以下警告,该警告是由 -Wextra 启用的(我已用 BaseClass 替换了该类的实际名称):

warning: base class ‘class BaseClass’ should be explicitly initialized in the copy constructor

我的问题是:我该如何禁用此警告?例如,-Wextra 还启用 -Wuninitialized,但我可以通过传递 -Wno-uninitialized 作为编译器标志来覆盖这个简单的操作。关于复制构造函数的警告是否有类似的内容?我无法在 g++ 手册页或任何其他论坛帖子中找到答案。

I'm using g++ version 4.2.1 with -Wextra enabled. I'm including a header from a library, and I keep getting the following warning about a class in the library, which is enabled by -Wextra (I've replaced the class's actual name with BaseClass):

warning: base class ‘class BaseClass’ should be explicitly initialized in the copy constructor

My question is: how can I disable this warning? For example, -Wextra also enables -Wuninitialized, but I can override that simple by passing -Wno-uninitialized as a compiler flag. Is there anything similar for the warning about the copy constructor? I wasn't able to find the answer in the g++ manpages or in any other forum posts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒浓于脸红 2024-10-20 11:04:09

给定:

class BaseClass
{
public:
    BaseClass();
    BaseClass(const BaseClass&);
};

class DerivedClass : public BaseClass
{
public:
    DerivedClass(const DerivedClass&);
};

此复制构造函数:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // warning: no BaseClass initializer!
{
}

实际上意味着相同:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // Default construct the base:
  : BaseClass()
{
}

如果这确实是您的意思,则可以放入像上面这样的默认构造函数初始值设定项,并且警告将会消失。但编译器建议你实际上可能想要这个:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // Copy construct the base:
  : BaseClass(obj)
{
}

Given:

class BaseClass
{
public:
    BaseClass();
    BaseClass(const BaseClass&);
};

class DerivedClass : public BaseClass
{
public:
    DerivedClass(const DerivedClass&);
};

This copy constructor:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // warning: no BaseClass initializer!
{
}

Really means the same as:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // Default construct the base:
  : BaseClass()
{
}

You can put in a default-constructor initializer like the above if that's really what you mean, and the warning will go away. But the compiler is suggesting that you might actually want this instead:

DerivedClass::DerivedClass(const DerivedClass& obj)
  // Copy construct the base:
  : BaseClass(obj)
{
}
青瓷清茶倾城歌 2024-10-20 11:04:09

根据 http://gcc.gnu.org/onlinedocs/gcc/Warning-Options .html(搜索 Wextra)是 -Wextra 的固有部分,不能单独禁用(例如,它没有按其单独列出)自己的 -W 选项)。

看来您能做的最好的事情就是要么将库的使用隔离到禁用 -Wextra 的一个文件,要么根本不单独使用 -Wextra启用其所有组件(从该链接)。

According to http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html (search for Wextra) that is an inherent part of -Wextra and can't be disabled separately (e.g. it isn't listed separately by its own -W option).

It looks like the best you can do is either isolate the use of the library to one file on which you disable -Wextra or don't use -Wextra at all and individually enable all its components (from that link).

半岛未凉 2024-10-20 11:04:09

如果这不是一个真正的问题,并且您无法更改库(我猜您不能或者您已经这样做了),您可以使用 GCC 诊断编译指示

If it's not a real problem, and you can't change the library (I guess you can't or you'd have done so), you can disable warnings temporarily using the GCC diagnostic pragma.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文