lundi 2 avril 2018

Linker error for different declarations of default constructors

I have been playing around with default constructors and noticed a weird behavior (from my point of view).

When I declare A() = default, I get no linker error.

struct A
{
  int a;
  A() = default;
};

A a; // no linker error

However, when I declare A(); I get it.

struct A
{
  int a;
  A();
};

A a; // linker error - undefined reference to `A::A()`

Questions:

  1. What's the difference between the two?

  2. And if A(); produces a linker error, why is it supported in the first place? Any practical applications?

Aucun commentaire:

Enregistrer un commentaire