Home > Mobile >  How to correctly include classes in c ?
How to correctly include classes in c ?

Time:12-31

I would like some clarification about including classes in c . I don't know the good way to do it because i don't know the differences between theme

#pragma once
#include "B.h" // Is it enough to include like this? ? 

class B; // what this line does ? 
class A : public B // what public B does here ?
{
    // .......
};

So how do i know if i should use class b or public B or juste a #include "B.h" ? I hope i was clear enough and thanks for the help

CodePudding user response:

#include "B.h" // Is it enough to include like this? ?

Yes.

class B; // what this line does ? 

This is an alternative to including B's header. Doing both is redundant.

It gives you less freedom. It allows you to create pointers and references to B, but not create instances of it, nor inherit from it, nor access its members.

On the other hand, class B; compiles faster, and can be used to break circular include dependencies.

class A : public B // what public B does here ?

Class A inherits from class B, publicly. Read about inheritance in your favorite C book.

  •  Tags:  
  • c
  • Related