MoonBun
MoonBun

Reputation: 4402

C/C++ Forward declaration vs. Include

What is happening when you include some file and what is happening when you forward declare some function/class? If two files include the same file will the first one success to read all the function the second will fail but still be able to use the functions??

What happens when I forward declare some function? Is this function now "saved" and I can use it anywhere or it's known only for the same file? then why two files with include(to a file with guards) will work?

Can I just include every thing in the main and won't bother any longer?

EDIT:

And why the cpp files should include their headers?? What If i won't include them?

Upvotes: 0

Views: 3704

Answers (3)

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30845

Short answer: Forwarding a class/function allows the compiler to not actually have to compile the entire class/function unless needed.

Long answer: Forwarding a class/function is just like declaring a class/function without defining it. You're promising to define it later, but for now you just want to inform the compiler it exists. You usually do these forward decelerations in header files. This generally results in faster compile times because in .cpp files that include your header, only those that actually need the class you've forwarded and include it's appropriate header file need to actually compile that included class's code.

Upvotes: 0

K-ballo
K-ballo

Reputation: 81399

What is happening when you include some file and what is happening when you forward declare some function/class?

When you include a file, its contents get "copy and pasted" into the inclusion source by the preprocessor. When you forward declare a function/class you are declaring an incomplete type, letting the rest of the translation unit know that a function/class with that name exist and making it usable within context where an incomplete declaration is allowed.

If two files include the same file will the first one success to read all the function the second will fail but still be able to use the functions??

If the included file includes proper include guards, the second inclusion within the same translation unit will be effectively a no-op. If two different source files include that same header file, the full content will be included in both files.

What happens when I forward declare some function? Is this function now "saved" and I can use it anywhere or it's known only for the same file? then why two files with include(to a file with guards) will work?

The function can only be used within the translation unit that contains the forward declaration. Generally each source file (.cpp) is a different translation unit, macro definitions (those of the header guards) as well as declarations/definitions are valid within that translation unit. Header guards prevent the same header file from being included more than once within the same translation unit, to prevent multiple declaration errors.

Upvotes: 4

Mooing Duck
Mooing Duck

Reputation: 66981

What is happening when you include some file and what is happening when you forward declare some function/class?

When you include a file, the preprocessor effectively copy pastes the entire included file into the file doing the includeing. When you forward declare a function/class, you're telling the compiler that it exists, but you don't need the entire header file. This is required when you have circular dependancies, and and greatly reduce compile times in other places.

If two files include the same file will the first one success to read all the function the second will fail but still be able to use the functions??

If the same file gets included twice in one translation unit (.cpp file), then the both will "succeed", but if the header has include guards of any sort, nothing will be loaded the second time, because the preprocessor has already "copied" it into the translation unit, and to do it a second time would make duplicates of everything, which would be a bug. So all files involved can use all the functions in all the headers included up to that point.

What happens when I forward declare some function? Is this function now "saved" and I can use it anywhere or it's known only for the same file? then why two files with include(to a file with guards) will work?

Yes, if you forward declare a function/class in a header, it can be used by any other file that includes that header.

Can I just include every thing in the main and won't bother any longer?

Probably. Once you get to more complicated examples, you'll end up with circular dependancies, which require certain things to be declared and/or defined in a certain order. Other than that, yes. You can include everything in main and keep it simple. However, your code will take FOREVER to compile.

And why the cpp files should include their headers?? What If i won't include them?

Then that .cpp file won't know that anything else exists outside of itself. Not very useful.

Upvotes: 0

Related Questions