Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8786459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:37:59+00:00 2026-06-13T21:37:59+00:00

Possible Duplicate: reincluding header in implementation What I am wondering is that it is

  • 0

Possible Duplicate:
reincluding header in implementation

What I am wondering is that it is common practice to not use using namespace xxx in a header file as not to pollute the global namespace.
How does this go for #includes?

If I have foo.h and foo.cpp.:

//Foo.h
#ifndef FOO_H_
#define FOO_H_

#include <string>

class Foo
{
  public:
    Foo(std::string * a, std::string * b);
    virtual ~Foo();
};

#endif /* FOO_H_ */

//Foo.cpp
#include <string>
#include <Foo.h>
Foo::Foo(std::string * a, std::string * b)
{
  // TODO Auto-generated constructor stub

}

Foo::~Foo()
{
  // TODO Auto-generated destructor stub
}

Would I really need to #include <string> in both files? Would including it only in the .h or .cpp suffice? (I know both will work, but what is advisable?)


edit, a bit more background information regarding my question.
If I would be using certain classes in my header file (either as variables or method arguments) I would forward declare them in the header file and only include the header file itself in the source file. But this will not work for most STL libs because you can’t forward declare class-templates?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:38:00+00:00Added an answer on June 13, 2026 at 9:38 pm

    … because you can’t forward declare class-templates?

    class templates can be forward declared – like non template classes:

    // forward declaration
    template <typename T>
    class C;
    

    However, as @AlexandreC stated in comments, for std::string it would be quite complicated, because std::string is typedef from template <typename,typename,typename> basic_string;.

    I write it would be complicated, if it would be allowed. And it is not allowed, see:

    • http://www.gotw.ca/gotw/034.htm
    • https://stackoverflow.com/a/10290176/1463922

    According to the C++11 standard, 17.6.4.2.1:

    The behavior of a C++ program is undefined if it adds declarations or
    definitions to namespace std or to a namespace within namespace std
    unless otherwise specified.

    So, no choice but include <string> in header file for std::string.


    For your main question – I would include in source and header files, unless I was pretty sure it would be always included in header file and never removed…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: Difference of create Index by using include column or not using Edit:
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: how to delete a file? My application first reads the fields in
Possible Duplicate: What is 0x10 in decimal? I notice that Console.WriteLine(18); writes 18, but
Possible Duplicate: Where can I find a web-project “security checklist?” i was just wondering
Possible Duplicate: Why is String.Concat not optimized to StringBuilder.Append? One day I was ranting
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Why is require_once so bad to use? When PHP is including a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.