Disable warning about explicitly initializing base constructor inside copy constructor of derived class

后端 未结 3 606
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 15:05

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 enable

相关标签:
3条回答
  • 2021-01-22 15:46

    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).

    0 讨论(0)
  • 2021-01-22 15:47

    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.

    0 讨论(0)
  • 2021-01-22 15:48

    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)
    {
    }
    
    0 讨论(0)
提交回复
热议问题