Class C ++ Doubt

6

I would like to understand why some classes in C ++ have the following declaration:

class Bla;

class Ble
{
 ....
}

My question is about the Bla class, it does not make sense as far as I can see.

    
asked by anonymous 11.10.2016 / 18:35

1 answer

7

This is called forward declaration . It is required to allow cyclic references between classes. It is possible to make the Ble class have a pointer to an object of class Bla and then make the Bla class have a pointer to an object of class Ble :

class Bla;

class Ble
{
    Bla *bla;
};

class Bla
{
    Ble *ble;
};

The first line is to declare that there will be a class called Bla , but not yet define its contents. With this it is already possible to declare pointers and references to objects of this class. At a later time it is necessary to define the class content.

In addition to solving the problem of cyclical references, this also avoids the need to give #include to get the definition of classes when you only want to create pointers or references. In this case, for example, instead of giving #include "bla.h" it is only possible to make forward declaration of class Bla . Doing so may leave the build process a bit faster.

    
11.10.2016 / 18:42