Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
Incomplete type

Incomplete type

01 Oct 2021

An incomplete type tells the compiler that a type with this name exists but it does not tell anything about how a type is implemented. For example, it doesn't tell what functions, variables are there. Usually, these types are fully defined later, so this declaration is often called a forward declaration.

Incomplete types include:

  • the void type;
  • structs, classes, and unions, for which an implementation has not yet been defined;
  • unknown size arrays;
  • arrays of elements of an incomplete type.

For the type to become complete (fully defined), we must specify the missing information. It is also worth noting the void type, which cannot be complete at all.

Limits when using incomplete types

An incomplete type does not tell the compiler anything about its internal structure. Thus, we cannot compile code that tries to access its contents. It is also impossible to perform operations that require knowledge of the exact type size. To do this, we need to know the size of the types that are contained in the required class.

Examples of incomplete types and ways to fully define them

We can obtain an incomplete type by using the following language constructs:

Forward - declaration:

class MyType;

Pointer to an unknown type:

struct MyType *myPtr;

An array containing elements of an incomplete type (even if the number of elements is known):

MyType b[10];

In all these cases, for a complete definition of the type, we are obliged to provide an implementation of the 'MyType' type somewhere. For example, this:

class MyType {
  int someNumber;
}

In this case, all restrictions imposed on incomplete types will be removed.

The situation with arrays of indeterminate size deserves a separate explanation since there are several nuances when using them. For example:

extern int a[];        // Incomplete type (an array of unknown 
   // size with the elements of the 'int' type)
int b[] = { 1, 2, 3 }; // Complete type 
                       // (an array of three values of the 'int' type)
int c[10];             // Complete type

Also, references and pointers can be created to arrays of unknown size, but in C++ they cannot be initialized (or assigned) by pointers to arrays with a known size. This restriction is absent in the C language because pointers to ordinary arrays and to arrays of unknown size are compatible there, and therefore can be freely converted and assigned in both directions.

extern int a[];
int (&a1)[]  = a;    // OK
int (*a2)[]  = &a;   // OK
int (*a3)[2] = &a;   // Error in C++, but correct in C
 
int b[] = {1, 2, 3};
int (&b1)[] = b;     // Error
int (*b2)[] = &b;    // Error in C++, but correct in C
Popular related articles

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam