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:
- 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.
- It works for line 2. Understood here. Through pointer C++ does not need the class definition.
- 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