Where should I #include the header files

Sep 5, 2014 at 12:47pm
To understand my question have a look at the code first:

Foo.h

1
2
3
4
class Foo
{
	void bar();
};


Foo.cpp
1
2
3
4
void Foo::bar()
{
	std::cout << "Foo::bar()" << '\n';
}


Where should I #include <iostream> , in Foo.h or in Foo.cpp?

I think it that it makes sense to #include <iostream> only in Foo.cpp since it is used there and it is not required anywhere in the declaration of class Foo.

Thanks in advance to anyone who replies!
Sep 5, 2014 at 1:17pm
You're correct that the .cpp file would be the best place since the .h file never uses the class. IMO you should never include unnecessary files. But you should always include all necessary files, never rely on some other file to include a required file.

Sep 5, 2014 at 1:22pm
@jib

Thank you so much for the quick reply!
Sep 5, 2014 at 1:24pm
Topic archived. No new replies allowed.