samedi 17 juillet 2021

C++ using class A example : Why vector va works but not A a on a forward declaration? [duplicate]

The example is as per below:

class A;

class B {
private:
  A a; // line 1 - failed
  A *b // Line 2 - ok
  vector <A> va; // Line 3 - OK
}

class A {
}

Understanding:

  1. Line 1 failed because it needs to know the size of A. Hence it needs class A definition. The forward declaration will not work in this case.
  2. It works for line 2. Understood here. Through pointer C++ does not need the class definition.
  3. However it also works for line 3. Why? Is it because the elements in vector are accessed through pointers? I know that through pointers C++ does not need the class definition then.

Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire