Warning: definition of implicit copy constructor is deprecated

前端 未结 3 1284
终归单人心
终归单人心 2020-12-25 13:20

I have a warning in my C++11 code that I would like to fix correctly but I don\'t really know how. I have created my own exception class that is derived from std::runt

3条回答
  •  伪装坚强ぢ
    2020-12-25 13:46

    Now I could get rid of that warning by simply removing the virtual destructor but I always thought that derived classes should have virtual destructors if the base class (in this case std::runtime_error) has a virtual destructor.

    You thought wrong. Derived classes will always have virtual destructor if you define one in base, no matter if you create it explicitly or not. So removing destructor would be simplest solution. As you can see in documentation for std::runtime_exception it does not provide it's own destructor either and it is compiler generated because base class std::exception does have virtual dtor.

    But in case you do need destructor you can explicitly add compiler generated copy ctor:

    MyError( const MyError & ) = default;
    

    or prohibit it making class not copyable:

    MyError( const MyError & ) = delete;
    

    the same for assignment operator.

提交回复
热议问题