0

I can't understand how the copy constructor was called without previous declaration of a.

I understood that the copy constructor takes a reference of A but I did not understand how this reference is initialized.

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
class A {
   public:
    A() = delete;
    A(A&) = default;
    void operator = (A&) {}
  private:
    int x;
};
int main()
{
    A a(a);
}
Haj Ayed Amir
  • 187
  • 4
  • 14
  • related/maybe dupe: [Calling copy constructor on yourself](https://stackoverflow.com/q/28590937/2602718) – scohe001 May 03 '19 at 15:50
  • *"without previous declaration of a"* There is a declaration of `a` *juuuust* before it's used in `(a)`. It's not initialized yet, but it's declared. – François Andrieux May 03 '19 at 15:52
  • But I deleted the default constructor, how the first a was declared? – Haj Ayed Amir May 03 '19 at 15:53
  • 1
    @HajAyedAmir The `a` is used before being initialized, but after it's declared. Initialization and declaration are separate things, but you often see them together. Edit : You have undefined behavior here because you will try to copy an uninitialized `A::x`. If you remove `int x;` you will no longer have undefined behavior, but you will have a useless class. – François Andrieux May 03 '19 at 15:54
  • What I understand now is that the copy constructor initializes the object and doesn't declare it. Can you share (docs or links) about how objects are declared in C++. Thanks – Haj Ayed Amir May 03 '19 at 15:59

0 Answers0