Reduce iostream compilation dependency by using iosfwd

30 03 2008

Icon Description
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 );
};

Icon How Can I Do It?
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 );
};
Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: