简体   繁体   中英

What does “class example;” mean in a Header File?

I've a question about something what really confuses me!

Let's watch this code righ here:

#ifndef HEADER_H
#define HEADER_H
#include <iostream>

class example;
class anything_else;

class A {
      public:
           A();
};

...

What does class example; and class anything_else; mean, while class A {}; gets declared? Inside the CPP File i saw definitions like void example::release() { ... } and so on...

I'm really confused, does anyone have a example with class example; ... ?

What does class example; and class anything_else; mean

They are declarations of the classes example and anything_else . They tell the compiler that those are valid class names. This kind of declaration is informally referred to as "forward declaration".

while class A {}; gets declared?

class A gets defined .

Inside the CPP File i saw definitions like void example::release() { ... } and so on...

That's the definition of the function example::release .

Somewhere in the definition of example , there's the declaration of the function:

class example
{
// ...
    void release(); // declaration
};

If example is only declared and not defined, then the definition of example::release results in a compilation error.

if you want to have member variables of a type that have not yet been included you do a forward declaration of the type.

class X;

forward declares the class X so you can have aeg a pointer to that class in our class declaration.

class Y
{
...
  X* p;
};

later you must supply the definition of the class somewhere in your .cpp file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM