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

  • SEARCH
  • Home
  • 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 8268691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:58:28+00:00 2026-06-08T05:58:28+00:00

Scenario: foo.h: #include <vector> class foo { public: std::vector<int>* getVector(); /* … other methods

  • 0

Scenario:

foo.h:

#include <vector>

class foo {
  public:
  std::vector<int>* getVector();

  /* ... other methods declarations ... */

}

foo.cpp:

#include "foo.h"
#include <vector>

/* ... other methods definitions using std::vector ... */

std::vector<int>* foo::getVector() {
  return new std::vector<int>();
}

I want .cpp to be independent of any possible future changes in the header. If for whatever reason the interface of the class changes and the dependency from <vector> can be eliminated, I risk that other methods in the .cpp also lose that inclusion.

Is it correct to repeat the inclusion of <vector> in both the .cpp and .h? Does this practice make sense or should I just rely on the inclusions made in the header?

  • 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-08T05:58:30+00:00Added an answer on June 8, 2026 at 5:58 am

    Include what you need, and nothing more.

    Including the same header file across multiple .h files and multiple .cpp files is not a problem in itself. Header guards are effective at mitigating problems from including files multiple times.

    If you start trying to avoid including the same file multiple times, it can actually be negative as it usually leads to a “mega-include file” which includes everything you need in the entire project. This is bad, because one change to any header file causes everything to re-compile.

    If you are worried about a .h/.cpp file both including the same file, then follow these guidelines:

    • If the include is not needed in the header file, only include it in the CPP
    • If a class declaration is needed in the header file (but not used), use forward declaration in the .h file and include it in the CPP file.
    • If you actually use the include within the header file, include it in the header file and not the CPP.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Original Question Consider the following scenario: public abstract class Foo { public string Name
I have the following scenario public class Foo { public Bar FooBar { get;
If i have the following scenario: public class Foo extends Baz{ private String banana
I have the following scenario, class foo { ... private: char *_test; }; void
In particular, I'm thinking of a scenario like this: unsafe struct Foo { public
I have this following scenario namespace A { //... class A { public: static
I have a scenario where I have a parent class with some definitions we
I have the following scenario: class A { string Foo; } Class B {
Scenario 1: Code: int main(){ int a = 12345678; if(isdigit(a)){ printf(ok: foo\n); } else{
I have the following scenario: class Foo { } class Foo<T> : Foo {

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.