What is an Incomplete Type in C++?

An incomplete type in C++ is not allowed. The following code will produce a compilation error: int i = 1; // We’re declaring an integer variable called “i” int* j = &i; // And then we assign it to point to the value of variable “i” (declared above) j++; // Do something with this pointer, but we don’t know what yet! The following code, however, will compile successfully: int* j = NULL; // We’re declaring a pointer variable called “j” and assigning it to point to nothing.

apple, computer, desk @ Pixabay

In the first example above, an incomplete type is being created (a reference) by using a ‘&’. In the second case where we set our pointer to null (), no object is created at all so there’s never anything that could be considered incomplete. The compiler knows this from context when parsing your program and allows the operation in line three of both examples. It would complain about an incomplete type if you were doing things like these instead: – int i

LEAVE A REPLY

Please enter your comment!
Please enter your name here