Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am expecting error that no default constructor as it is deprecated but i am not getting any error?could anyone tell?

As per cppreference doc:

The generation of the implicitly-defined copy constructor is deprecated if T has a user-defined destructor or user-defined copy assignment operator.

But i am not getting any error like default constructor not present.Please correct me if i am wrong.

#include <iostream>

class Demo
{
    int val = 100;
public:
  ~Demo()
  {
      std::cout << "destructor called\n";
  }

  Demo& operator= (Demo obj)
  {
      std::cout << "copy assignment operator called\n";
  }
};

int main()
{
    Demo obj1;//I am expecting error here : no default constructor present as it is deprecated
    Demo obj2(obj1);
    return 0;
}

//output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

destructor called
destructor called

>Solution :

The quote talks about the implicitly-defined copy constructor, i.e. the implicit constructor of the form

Demo(const Demo&);

not the implicitly-defined default constructor, i.e. the constructor of the form

Demo();

The latter is what you are using in Demo obj1; and the quote doesn’t apply to it.

The line Demo obj2(obj1); should however generate a deprecation warning, because it does use the implicitly-declared copy constructor, which is deprecated since C++11.

However, the compilers seem to have decided to not warn about this deprecation in general. For example GCC requires -Wextra to emit it. Also, the deprecation has been around for a long time, so it might be questionable whether it will ever actually be removed. Nonetheless, a class declaring a copy assignment and/or destructor explicitly is almost always broken if it doesn’t also define a copy constructor explicitly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading