Sign in to follow this  

[C++] Will this generate errors?

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I have a class A in the header "A.h" and the class inherits from class B in header "B.h". In the header "C.h" I declare a pointer to a B-object. If I in the header "A.h", thus creating a pointer to the base-class which I'm inheriting from, will this generate errors? My guesses are no, but by God, I'll be glad if it did. Can I get any other advice on using headers correctly? (Like putting #ifndef A_H #define A_H ... #endif in the beginning and end) What are incorrect combinations? Oh, and if anyone can give me tips on what may cause an error where a class isn't declared, even though the correct header is included above the line of error. (Presumably the class is correctly defined)

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Read this for your first question and this for one of the trickier possible answers (out of many) for your second.


I am your humble servant! You just helped me solving a problem that has genereted 18 constant messages for several hours during a number of weeks! Probebly you can imagine the feeling. I am in debt! Thank you!!

Share this post


Link to post
Share on other sites
jpetrie, nice explanation about the master headers. you illustrate a good point. but, as you also conclude at the bottom, they do still have their place...so perhaps it would be good as a follow up to describe how they can be used in an extremely beneficial way as well.

as any project grows in complexity, the interdependencies generally tend to grow in complexity as well..the compilation time increases, and the number of headers you have to include in each new translation unit you create grow as well. the more places you have to include multiple files, where the order of those includes matters, the more chances to mess up there are.

by using a master precompiled header, a lot of this complexity can be eliminated. the key is to not treat all headers equally. I have a set of "core" headers that are completely independent from each other, and hence would be shared between multiple completely different projects. for example, this would be math libraries, API libraries, generic data structures and algorithms, etc. all of these can be put into the main precompiled header, and in general, should all be independent from each other.

as a result, the actual project-specific headers have in general just 1 include file, making them less error prone, simpler to read, and much much faster to compile. the only restriction is that you MUST include that precompiled header in each new file you make with visual studio (although there are technically some ways to hack around this requirement by toggling MS pragmas)

Share this post


Link to post
Share on other sites

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this