template <class T>
class Nodo
{
protected:
T d;
Nodo *h[2] = {NULL};
public:
Nodo(T dato = T()) { d = dato; }
};
Even if the class Nodo
is an incomplete class in the declaration point of h
, because it is still being processed, you can always declare pointers, references and functions that receive or return objects by value of this incomplete type. That is, this code compiles (although I have not tried it):
#include <iostream>
class A;
A* a = nullptr;
// A* a2 = new A; // [†]
struct B
{
A& ra; // [†2]
};
A f(A);
A& f(A&); // [†3]
// A& ra = *a; // [†4]
int main()
{}
[†]: That does not compile because, even if it is valid to declare pointers to an incomplete type, you can not create any object of that type when not knowing yet what builders you have, and above all, you do not yet know the size of A
( sizeof(A)
).
[† 2]: The case A&
I have exemplified as an attribute and not as a local or global variable because you can not declare the reference as a variable (be it global or local), since references must be mandatorily defined in your declaration (for example, ra = *a
), that is, you can not declare a reference, and assign a value to it later. However, since A
is an incomplete type, you have no way to "get" the object you want to reference. That is, *a
would not compile by A
is an incomplete type and we do not know what an object of type A
is.
However, you can declare it as an attribute, since there you are not forced to initialize it. That will necessarily happen in the constructor, which you could not implement yet until A
is not defined.
[† 3]: Function parameters are the only places where you can declare objects of an incomplete type (and not just references or pointers), since, at that point, you do not need to know its size ( sizeof(A)
) nor its content. Of course, you could not implement the function until A
is defined later in the code and copy constructors are already available, etc.
[† 4]: Again, *a
can not be applied if you still do not know A
. I do not know exactly the reasons, but usually it's usually because to perform that kind of actions the compiler needs its sizeof
.