Gold mine of Visual C++ tricks!
Posts tagged cin
Reduce iostream compilation dependency by using iosfwd
Mar 30th
![]()
For making our classes compatible with cin and cout, we usually over load << and >> operators. And if such classes are the part of our modules interface, the iostream header file should be added to the interface header of our module. It makes compilation overhead for the classes those use our exported class. Please see the following code block.
#include <iostream>
class IMyInterface
{
public:
// Overloaded << operator.
std::ostream& operator<<( std::ostream& os );
};
![]()
You can easily reduce the compile time dependency of iostream header by using iosfwd. It contains forward declaration of several template classes defined in iostream. Please check MSDN for more details. Don’t forget to include iostream in your cpp files, since iosfwd contains just forward declarations.
#include <iosfwd>
class IMyInterface
{
public:
// Overloaded << operator.
std::ostream& operator<<( std::ostream& os );
};