Is this a valid copy of Ctor?

Interestingly, something is wrong with the copy constructor function below?

class A
{
   private:
      int m;
   public:
      A(A a){m=a.m}
}
+3
source share
3 answers

Two things:

  • Copy constructors must accept links as parameters, otherwise they are infinitely recursive (in fact, the language will not allow you to declare such constructors)

  • It does nothing that the ctor copy does not do by default, but it does it poorly - you should use the initialization lists in the ctor copy where possible. And if the default ctor copy does what you want, don’t be tempted to write the version yourself - you will probably make a mistake and you will need to maintain it.

+17
source

There are 3 problems.

-, ";" m = a.m, .

-, , - .

-, , . , , , :

A (const A a): m (a.m) {}

+7

, , . , ( ), . , , . :

A(A const& a)
  : m(a.m)
{}  
+5

Source: https://habr.com/ru/post/1736513/


All Articles