definition of implicit copy constructor is deprecated

Hello,

I have a compilation error

definition of implicit copy constructor for
'MyClass' is deprecated because it has a user-declared destructor [-Wdeprecated]

I did not have this before. Is this related to C++11?

Thanks,
M

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /* header file */
  class MyClass
  {
    private:
    /* variables */
      int a_;
      int b_;

    public:
      MyClass();
      ~MyClass();
  };

  /* in .cpp file */

  MyClass::MyClass() :
  {
     a_ = 2
     b_ = 2
  }

  MyClass::~MyClass()
  {
    /* do something */
  }
> Is this related to C++11?

Yes.

By default, a class has 5 operations:
copy assignment
copy constructor
move assignment
move constructor
destructor

If you declare any of those you must consider all and explicitly define or default the ones you want. Think of copying, moving, and destruction as closely related operations, rather than individual operations that you can freely mix and match - you can specify arbitrary combinations, but only a few combinations make sense semantically.

If any move, copy, or destructor is explicitly specified (declared, defined, =default, or =delete) by the user, no move is generated by default. If any move, copy, or destructor is explicitly specified (declared, defined, =default, or =delete) by the user, any undeclared copy operations are generated by default, but this is deprecated, so don't rely on that.

...

I strongly recommend that if you declare one of these five function, you explicitly declare all.

http://www.stroustrup.com/C++11FAQ.html#default
Topic archived. No new replies allowed.