Tom N
Tom N

Reputation: 104

C++ Library Inclusion Guards

Just a question of style, or possibly even a malpractice I am unaware of.

I am currently writing my first piece of software, which will be used and reviewed by people other than myself. When I am writing my code and calling my headers, is it bad practice to call the same header multiple times across files.

For example

exampleClass.h

#ifndef BUG_H
#define BUG_H
#include<string>

class Bug{
private:
    int bug_id; //6 digit int
    Date creation_ts; //Date object containing time of creation
    std::string short_desc; //Short description of bug
    std::string classification; //Catagory of bug_id
    std::string product; //What product is the bug regarding
    std::string component
}

#endif

anotherExample.h

#ifndef ANOTHEREXAMPLE_H
#define ANOTHEREXAMPLE_H
#include<string>

class Pug{
private:
    int bug_id; //6 digit int
    Date creation_ts; //Date object containing time of creation
    std::string short_desc; //Short description of bug
    std::string classification; //Catagory of bug_id
    std::string product; //What product is the bug regarding
    std::string component
}

#endif

Is there anything wrong with including string twice in two different header files if both have dependencies? Will this cause errors later in the software's life?

Upvotes: 2

Views: 1670

Answers (3)

Arnav Borborah
Arnav Borborah

Reputation: 11789

If the two files are unrelated, eg not included both in the same source, then you have no choice. Otherwise, It doesn't really matter, since <string> would get included anyway, if you included one file after the other. One of the files would still need it. But still, if one file ever needs a file, ALWAYS include it. There is always the risk that one time someone may forget to include the other file, and the code won't compile. Don't take the risk, trusting clients.

In addition, std::string has include guards as well, so no need to worry about multiple inclusion. Also, for safety of inclusion, you can do this fr your headers:

#pragma once
#ifndef HEADER_H
#define HEADER_H
//.....Code
#endif

You could always #pragma or #define, (as in 1 or the other), but putting both guarantees header guards, since old compilers don't support #pragma once

Upvotes: 2

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140168

You have to include string if you are using it in your class. Since string is also protected against multiple inclusions it is fine.

BTW there's a better way than the ifdefs to avoid multiple inclusions:

#pragma once

Upvotes: 1

Barry
Barry

Reputation: 302862

Is there anything wrong with including string twice in two different header files if both have dependencies? Will this cause errors later in the software's life?

No. In fact, you should include every header file that you actually have dependency on. You shouldn't rely on some other header being included that includes your dependency. It's failure to include all the headers you need that causes errors. If your class needs std::string, it should include <string>. Period.

All headers should have include guards (whether of the #ifndef or #pragma once varieties). Certainly the ones in your standard library implementation do. So the downside of extra includes is just marginal extra preprocessing time, with the upside being guaranteed compiling code.

Upvotes: 1

Related Questions