Pergunta

I was reading through VTK source files, I noticed statements as followed in some of the header files.

class vtkArray;
class VTKIOCORE_EXPORT vtkArrayWriter : public vtkWriter
{
    //Interface description here.
}

I was puzzled by the statement 'class vtkArray;'. What is the meaning of this statement? What is it being used for, when is a good time to use it?

This code is from a header file.

Foi útil?

Solução

That is a forward declaration. It is mostly used when you need to declare the class type without specifying its internal form. This is useful when dealing with pointers or references as statements like:

type* x;
type& x;

does not require the compiler to know the size of type.

Outras dicas

This looks like a forward declaration. http://en.wikipedia.org/wiki/Forward_declaration It is needed when you want to use a class before it is defined.

Is this in a header file? They are forward declaring the class vtkArray. This way they do not need to include the header file that contains that class. Then they can just include the header file in the .cpp file.

Edit: Link to explanation of forward declaration

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top